4

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

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

4

2 回答 2

2
  1. 为什么有理由super();

  2. DefaultTableModel可以添加Object[]Vector

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

  4. 可以从列表表模型行表模型开始

于 2012-11-08T07:45:12.663 回答
0

我可以建议你一个完整的表格模型示例来了解它是如何工作的。它还使用列表作为数据。最重要的是您需要扩展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"));
于 2015-04-29T14:40:48.153 回答