Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/376.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用AbstractTableModel在JTable中删除和添加行_Java_Swing_Jtable_Abstracttablemodel - Fatal编程技术网

Java 使用AbstractTableModel在JTable中删除和添加行

Java 使用AbstractTableModel在JTable中删除和添加行,java,swing,jtable,abstracttablemodel,Java,Swing,Jtable,Abstracttablemodel,我遇到了一个严重的问题,从JTable中删除行,我的代码与我尝试学习AbstractTableModel时看到的代码相同: import javax.swing.table.AbstractTableModel; public class MyTableModel extends AbstractTableModel { boolean DEBUG = true; input_Data input = new input_Data();

我遇到了一个严重的问题,从JTable中删除行,我的代码与我尝试学习AbstractTableModel时看到的代码相同:

import javax.swing.table.AbstractTableModel;

public class MyTableModel extends AbstractTableModel {

             boolean DEBUG = true;

            input_Data input = new input_Data();
            String[] columnNames = {"First Name",
                      "Last Name",
                      "Sport",
                      "# of Years",
                      "Vegetarian"};
            Object[][] data = {
                {"Kathy", "Smith",
                    "Snowboarding", new Integer(5), new Boolean(false)},
                {"John", "Doe",
                        "Rowing", new Integer(3), new Boolean(true)},
                {"Sue", "Black",
                            "Knitting", new Integer(2), new Boolean(false)},
                {"Jane", "White",
                                "Speed reading", new Integer(20), new Boolean(true)},
                {"Joe", "Brown",
                                    "Pool", new Integer(10), new Boolean(false)};


                    public int getColumnCount() {
                        return columnNames.length;                  
                    }

                    public int getRowCount() {
                        return data.length;
                    }

                    public String getColumnName(int col) {
                        return columnNames[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 row, int col) {

                        if (col < 2) {
                            return false;
                        } else {
                            return true;
                        }
                    }
                    public void removeRow(Row)
                    {

                       fireTableRowsDeleted(Row,Row);

                    }
                }
但问题是,当我打电话给removeRow时,什么也没发生!! 我想也许我也应该编辑数据,但如何编辑呢?
我是java新手,我真的对这个问题很感兴趣…

你还没有用这段代码删除任何东西。不要将二维数组用于数据nucleus。为该类的数据核心使用ArrayList,在removeRowint row方法中实际删除该列表中的数据行,然后调用fireTableRowsDeleted。。。方法。

使用DefaultTableModel。它已经支持removeRow方法。我不能,我的表中需要一个复选框,所以我不能使用DefaultTableModel。没有理由不能使用DefaultTableModel。您只需在TableModel中存储一个布尔值并重写getColumnClass。。。方法返回布尔值,表将选择适当的渲染器。没有理由仅仅为了重写类的单个方法而创建一个全新的类。非常感谢,我的问题解决了: