注册 登录
编程论坛 JAVA论坛

直接setRowCount(tableModel.getRowCount()+1);和addRow(new Object[] {});有何区别?

msl12 发布于 2015-11-14 12:15, 693 次点击
代码如下:
--------------------------------------------------
public ChangeTable() {
        this.tableModel = new DefaultTableModel(this.userInfo, this.titles);
        this.table = new JTable(this.tableModel);
        JScrollPane scr = new JScrollPane(this.table);
        JPanel toolBar = new JPanel();
        toolBar.add(this.addRowBtn);
        toolBar.add(this.removeRowBtn);
        toolBar.add(this.addColBtn);
        toolBar.add(this.removeColBtn);
        frame.add(toolBar, BorderLayout.NORTH); // 加入组件
        frame.add(scr, BorderLayout.CENTER); // 加入组件
        frame.pack();
        frame.setVisible(true);
        frame.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                System.exit(1);
            }
        });
        this.addRowBtn.addActionListener(this);
        this.removeRowBtn.addActionListener(this);
        this.addColBtn.addActionListener(this);
        this.removeColBtn.addActionListener(this);
    }

    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == this.addRowBtn) {
            //this.tableModel.addRow(new Object[] {});
            this.tableModel.setRowCount(tableModel.getRowCount()+1);    //这个和上一行的同样完成了加一行的功能,有什么区别?同理删除行使用这行
                                                                                      的类似代码内存上是否真的删除了?
        }
        }
    }
0 回复
1