qq_38828780 2021-10-29 12:58 采纳率: 100%
浏览 23
已结题

通过继承AbstractTableModel类实现组件控制,写了一个添加一列数据的函数,但是数据增加了但是表头没有变.

img

img

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.table.AbstractTableModel;


public class practice02 implements ActionListener{
    private MyTable mytable;
    public practice02(){
        JFrame frame = new JFrame();
        JPanel panel = new JPanel(new BorderLayout());
        JPanel panel2 = new JPanel(new FlowLayout(FlowLayout.CENTER,20,0));
        Container contentpane = frame.getContentPane();
        contentpane.setLayout(new BorderLayout());
        Object [] [] data = new Object[30][6];
        int sname,i;
        for(sname=0;sname<30;sname++){
            data [sname][0] = "同学" + String.valueOf(sname);
            data [sname][1] = "班级A";
            data [sname][2] = this.getScore();
            data [sname][3] = this.getScore();
            data [sname][4] = this.getScore();
            data [sname][5] = true;
            for(i=2;i<5;i++){
                if((int)data[sname][i]<90){
                    data [sname][5] = false;
                    break;
                }
            }
        }
        String[] names = {"姓名","班级","C语言","Java","数据结构","优秀"};
        mytable = new MyTable(data,names,2,4);
        JTable table = new JTable(mytable);
        JComboBox<String> comboBox = new JComboBox<String>();
        comboBox.addItem("班级A");
        comboBox.addItem("班级B");
        comboBox.addItem("班级C");
        table.getColumnModel().getColumn(1).setCellEditor(new DefaultCellEditor(comboBox));
        
        //下面这句不理解
        //table.setPreferredScrollableViewportSize(new Dimension(550, 30));
        
        //添加按钮
        JButton button;
        button = new JButton("加入行");
        button.addActionListener(this);
        panel2.add(button);
        button = new JButton("删除行");
        button.addActionListener(this);
        panel2.add(button);
        button = new JButton("加入列");
        button.addActionListener(this);
        panel2.add(button);
        button = new JButton("删除列");
        button.addActionListener(this);
        panel2.add(button);
        //添加界面
        panel.add(new JScrollPane(table),BorderLayout.CENTER);
        panel.add(panel2,BorderLayout.SOUTH);
        contentpane.add(panel);
        
        //设置界面
        frame.setSize(600, 470);
        frame.setTitle("JTable test");
        frame.setVisible(true);
        frame.addWindowListener(new WindowAdapter(){
            public void windowClosing(WindowEvent e){
                System.exit(0);
            }
        });

    }
    @Override
    public void actionPerformed(ActionEvent e){
        //按钮事件处理
        if(e.getActionCommand().equals("加入行")){
            this.mytable.addRow();
        }
        else if(e.getActionCommand().equals("删除行")){
            this.mytable.removeRow();
        }
        else if(e.getActionCommand().equals("加入列")){
            this.mytable.addCount();
        }
        else if(e.getActionCommand().equals("删除列")){
            this.mytable.removeCount();
        }
    }
    public static void main(String[] args){
        new practice02();
    }
    public Integer getScore(){
        return Integer.valueOf((int)(Math.random()*15)+85);
    }
    class MyTable extends AbstractTableModel{
        private Object[][] data;
        private String[] names;
        private int startIndex,endIndex;
        public MyTable(Object[][] data,String[] names){
            super();
            this.data = data;
            this.names = names;
            this.startIndex = 1;
            this.endIndex = names.length - 2;
        }
        public MyTable(Object[][] data,String[] names,int startIndex,int endIndex){
            super();
            this.data = data;
            this.names = names;
            this.startIndex = startIndex;
            this.endIndex = endIndex;
        }
        public void addRow(){
            Object[][] tempData = new Object[data.length + 1][names.length];
            int i,j;
            for(i=0;i<data.length;i++){
                tempData[i]=data[i];
            }
            tempData[i]= new Object[names.length];
            tempData [i][0] = "同学" + String.valueOf(i);
            tempData [i][1] = "班级A";
            for(j=startIndex;j<=endIndex;j++){
                tempData[i][j] = getScore();
            }
            data = tempData;
            setExcellent(i);
            fireTableDataChanged();
        }
        public void removeRow(){
            if(data.length<1)return;
            Object[][] tempData = new Object[data.length - 1][names.length];
            int i;
            for(i=0;i<tempData.length;i++){
                tempData[i] = data[i];
            }
            data = tempData;
            fireTableDataChanged();
        }
        //功能有问题
        public void addCount(){
            Object[][] tempData = new Object[data.length][names.length + 1];
            String[] tempNames = new String[names.length +1];
            int i,j;
            for(i=0;i<tempData.length;i++){
                for(j=0;j<names.length + 1;j++){
                    if(j<names.length-1){
                        tempData[i][j]=data[i][j];
                    }
                    else if(j==names.length-1){
                        tempData[i][j]=getScore();
                    }
                    else if(j==names.length){
                        tempData[i][j] = true;
                    }
                }
            }
            for(i=0;i<names.length-1;i++){
                tempNames[i] = names[i];
            }
            tempNames[i] = "附加学科"+String.valueOf(i-4);
            tempNames[i+1]=names[i];
            endIndex++;
            data = tempData;
            names = tempNames;
            for(i=0;i<data.length;i++){
                setExcellent(i);
            }
            fireTableDataChanged();
        }
        public void removeCount(){
            if(names.length<7)return;
            Object[][] tempData = new Object[data.length][names.length - 1];
            String[] tempNames = new String[names.length - 1];
            int i,j;
            for(i=0;i<tempData.length;i++){
                for(j=0;j<tempData[i].length;j++){
                    if(j==tempData[i].length-1){
                        tempData[i][j] = true;
                    }
                    else{
                        tempData[i][j] = data[i][j];
                    }
                }
            }
            for(i=0;i<tempNames.length-1;i++){
                tempNames[i] = names[i];
            }
            tempNames[i] = names[i+1];
            endIndex--;
            data = tempData;
            names = tempNames;
            for(i=0;i<data.length;i++){
                setExcellent(i);
            }
            fireTableDataChanged(); 
        }
        public int getColumnCount(){
            return names.length;
        }
        public int getRowCount(){
            return data.length;
        }
        public String getColumnName(int col){
            return names[col];
        }
        public Object getValueAt(int row, int col){
            return data[row][col];
        }
        public Class<?> getColumnClass(int c){
            return getValueAt(0, c).getClass();
        }
        public boolean isCellEditable(int rowIndex, int columnIndex){
            if(columnIndex == endIndex + 1) return false;
            else return true;
        }
        private void setExcellent(int row){
            int i;
            data[row][endIndex + 1] = true;
            for(i=startIndex;i<endIndex+1;i++){
                if((int)data[row][i]<90){
                    data[row][endIndex + 1] = false;
                    break;
                }
            }
            fireTableCellUpdated(row,endIndex + 1);
        }
        public void setValueAt(Object value, int row, int column){
            data[row][column] = value;
            if(column>=startIndex && column<=endIndex){
                int i;
                data[row][endIndex + 1] = true;
                for(i=startIndex;i<endIndex+1;i++){
                    if((int)data[row][i]<90){
                        data[row][endIndex + 1] = false;
                        break;
                    }
                }
                fireTableCellUpdated(row,endIndex + 1);    
            }
            fireTableCellUpdated(row,column);
        }
    }

}


  • 写回答

1条回答 默认 最新

  • 关注

    JTable要放到JScrollPane里面,否则表头不会显示。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 系统已结题 11月23日
  • 已采纳回答 11月15日
  • 修改了问题 10月31日
  • 创建了问题 10月29日

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀