Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/366.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 通过自定义表模型向jtable中添加行_Java_Swing_Jtable_Defaulttablemodel - Fatal编程技术网

Java 通过自定义表模型向jtable中添加行

Java 通过自定义表模型向jtable中添加行,java,swing,jtable,defaulttablemodel,Java,Swing,Jtable,Defaulttablemodel,我已经创建了一个扩展DefaultTableModel的表模型 public class TableModel extends DefaultTableModel { List<VariableDetails> data; public AttachedVariableTableModel(){ super(); this.data=Collections.synchronizedList(new ArrayList<VariableDetails>())

我已经创建了一个扩展DefaultTableModel的表模型

public class TableModel extends DefaultTableModel {
List<VariableDetails> data;
public AttachedVariableTableModel(){
super();        
this.data=Collections.synchronizedList(new ArrayList<VariableDetails>());
}

//method for adding rows
public void addRow(VariableDetails varDetails){

    this.data.add(varDetails);
    fireTableRowsInserted(this.data.size()-1,this.data.size()-1);
    }

}
但不能添加行。没有例外和错误。这里到底出了什么问题?我怎样才能解决这个问题?提前谢谢

  • 为什么会有
    super()

  • DefaultTableModel
    可以添加
    Object[]
    Vector

  • 必须重写
    AbstractTableModel
    ,而不是
    DefaultTableModel
    ,必须在方法中使用适当的
    fireXxxXxx()
    重写所有,否则视图中没有可见的内容(
    JTable

  • 可以从或开始


  • 请允许我向您推荐一个完整的表格模型示例,以了解其工作原理。它还使用列表作为数据。最重要的是,您需要扩展AbstractTableModel以使用您自己的变量来存储数据。这是一个完整的源代码示例

    import java.util.ArrayList;
    import java.util.List;
    
        import javax.swing.table.AbstractTableModel;
    
        public class MouseClickTableModel extends AbstractTableModel {
            /**
             * 
             */
            private static final long serialVersionUID = -1807522184370383957L;
    
            private final String[] columnNames = { "Sr", "X", "Y", "Delay (ms)",
                    "Comment" };
    
            public final Class[] mColTypes = { Integer.class, Integer.class,
                    Integer.class, Integer.class, String.class };
    
            private final List<MouseClick> data;
    
            public MouseClickTableModel(){
                data = new ArrayList<MouseClick>(10);
            }
    
            public int getColumnCount() {
                return columnNames.length;
            }
    
            public int getRowCount() {
                return data.size();
            }
    
            public String getColumnName(int col) {
                return columnNames[col];
            }
    
            public Object getValueAt(int row, int col) {
                final MouseClick currentRow = (MouseClick) data.get(row);
    
                switch (col) {
                case 0:
                    return currentRow.getSequNb();
                case 1:
                    return currentRow.getXcoo();
                case 2:
                    return currentRow.getXycoo();
                case 3:
                    return currentRow.getDelay();
                case 4:
                    return currentRow.getComment();
                }
    
                return new String();
            }
    
            public Class getColumnClass(int c) {
                return mColTypes[c];
            }
    
            /*
             * Don't need to implement this method unless your table's editable.
             */
            public boolean isCellEditable(int row, int col) {
                return false;
            }
    
            public void updateRow(Object value, int row, int col) {
    
            }
    
            /*
             * Don't need to implement this method unless your table's data can change.
             */
            @Override
            public void setValueAt(Object value, int row, int col) {
                MouseClick currentRow = null;
                if (row >= data.size()) {
                    // new data
                    currentRow = new MouseClick();
                    data.add(0, currentRow);
                }
                // update row
                else {
                    currentRow = (MouseClick) data.get(row);
                }
    
                switch (col) {
                case 0:
                    currentRow.setSequNb(((Integer) value).intValue());
                    break;
                case 1:
                    currentRow.setXcoo(((Integer) value).intValue());
                    break;
                case 2:
                    currentRow.setXycoo(((Integer) value).intValue());
                    break;
                case 3:
                    currentRow.setDelay(((Integer) value).intValue());
                    break;
                case 4:
                    currentRow.setComment(((String) value).toString());
                    break;
                }
                // update
                fireTableCellUpdated(row, col);
            }
    
            public MouseClick getData(int row) {
                return data.get(row);
            }
    
            public void addMouseClick(MouseClick mc) {
                insertMouseClick(getRowCount(), mc);
            }
    
            public void insertMouseClick(int row, MouseClick mc) {
                data.add(row, mc);
                fireTableRowsInserted(row, row);
            }
    
            public void removeMouseClick(int row) {
                data.remove(row);
                fireTableRowsDeleted(row, row);
            }
    
        }
    

    这里显示的代码是您的全表模型吗?因为您没有使用
    数据
    变量。向该变量添加
    数据
    无效。要更快地获得更好的帮助,请发布一篇文章。
    import java.util.ArrayList;
    import java.util.List;
    
        import javax.swing.table.AbstractTableModel;
    
        public class MouseClickTableModel extends AbstractTableModel {
            /**
             * 
             */
            private static final long serialVersionUID = -1807522184370383957L;
    
            private final String[] columnNames = { "Sr", "X", "Y", "Delay (ms)",
                    "Comment" };
    
            public final Class[] mColTypes = { Integer.class, Integer.class,
                    Integer.class, Integer.class, String.class };
    
            private final List<MouseClick> data;
    
            public MouseClickTableModel(){
                data = new ArrayList<MouseClick>(10);
            }
    
            public int getColumnCount() {
                return columnNames.length;
            }
    
            public int getRowCount() {
                return data.size();
            }
    
            public String getColumnName(int col) {
                return columnNames[col];
            }
    
            public Object getValueAt(int row, int col) {
                final MouseClick currentRow = (MouseClick) data.get(row);
    
                switch (col) {
                case 0:
                    return currentRow.getSequNb();
                case 1:
                    return currentRow.getXcoo();
                case 2:
                    return currentRow.getXycoo();
                case 3:
                    return currentRow.getDelay();
                case 4:
                    return currentRow.getComment();
                }
    
                return new String();
            }
    
            public Class getColumnClass(int c) {
                return mColTypes[c];
            }
    
            /*
             * Don't need to implement this method unless your table's editable.
             */
            public boolean isCellEditable(int row, int col) {
                return false;
            }
    
            public void updateRow(Object value, int row, int col) {
    
            }
    
            /*
             * Don't need to implement this method unless your table's data can change.
             */
            @Override
            public void setValueAt(Object value, int row, int col) {
                MouseClick currentRow = null;
                if (row >= data.size()) {
                    // new data
                    currentRow = new MouseClick();
                    data.add(0, currentRow);
                }
                // update row
                else {
                    currentRow = (MouseClick) data.get(row);
                }
    
                switch (col) {
                case 0:
                    currentRow.setSequNb(((Integer) value).intValue());
                    break;
                case 1:
                    currentRow.setXcoo(((Integer) value).intValue());
                    break;
                case 2:
                    currentRow.setXycoo(((Integer) value).intValue());
                    break;
                case 3:
                    currentRow.setDelay(((Integer) value).intValue());
                    break;
                case 4:
                    currentRow.setComment(((String) value).toString());
                    break;
                }
                // update
                fireTableCellUpdated(row, col);
            }
    
            public MouseClick getData(int row) {
                return data.get(row);
            }
    
            public void addMouseClick(MouseClick mc) {
                insertMouseClick(getRowCount(), mc);
            }
    
            public void insertMouseClick(int row, MouseClick mc) {
                data.add(row, mc);
                fireTableRowsInserted(row, row);
            }
    
            public void removeMouseClick(int row) {
                data.remove(row);
                fireTableRowsDeleted(row, row);
            }
    
        }
    
    JTable table = new JTable(new MouseClickTableModel());
    table.setPreferredScrollableViewportSize(new Dimension(500, 70));
    table.setFillsViewportHeight(true);
    MouseClickTableModel model = getTable().getModel());
    model.insertMouseClick(0, new MouseClick(0, Integer.valueOf(xValue),  Integer.valueOf(yValue), 2000, "comment"));