【问题标题】:Adding rows in to jtable by customized table model通过自定义表模型将行添加到 jtable
【发布时间】:2012-10-28 08:19:39
【问题描述】:

我创建了一个扩展 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);
    }

}

我尝试向已包含数据的表中添加行。

tModel.addRow(new  VariableDetails());

但无法添加行。没有例外和错误。这里到底有什么问题?我该如何解决这个问题?提前致谢。

【问题讨论】:

  • 这里显示的代码是您的全表模型吗?由于您没有使用 data 变量。向该变量添加data 无效
  • 如需尽快获得更好的帮助,请发帖SSCCE

标签: java swing jtable defaulttablemodel


【解决方案1】:
  1. 为什么会有super();

  2. DefaultTableModel可以加Object[]Vector

  3. 必须覆盖AbstractTableModel,而不是DefaultTableModel,必须覆盖所有get/set methods,在方法中使用正确的fireXxxXxx(),否则视图中看不到任何东西(JTable

  4. 可以以List Table ModelRow Table Model开头

【讨论】:

    【解决方案2】:

    我可以建议你一个完整的表格模型示例来理解它是如何工作的。它还使用列表作为数据。最重要的是您需要扩展 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);
            }
    
        }
    

    然后你只需要用你的 ui 设计更新你的模型。

    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"));
    

    【讨论】:

      猜你喜欢
      • 2020-03-24
      • 1970-01-01
      • 1970-01-01
      • 2015-01-25
      • 2012-11-30
      • 1970-01-01
      • 1970-01-01
      • 2017-12-19
      • 1970-01-01
      相关资源
      最近更新 更多