自定义表模型不起作用

时间:2015-03-17 20:24:33

标签: java swing jtable tablemodel abstracttablemodel

我写了一个自定义表模型。它的目标是将值绑定到列表。此列表在整个应用程序中使用。如果在表中进行了更改,则应用程序中的任何其他位置也会反映更改。我的问题是,我的表JTable不显示行。

这是我的第一个自定义表格模型,因此我不确定是否遗漏了某些内容。

使用以下代码创建表:

public CBTable(List<T> elements, Class<T> classType) {
    super(new BindingTableModel(elements));
    this.tableModel = (BindingTableModel<T>) getModel();
    this.classType = classType; 
    setBindings();
    repaint();
}

setBindings的一个例子是:

@Override
protected void setBindings() {
    addBinding(ProfessionalDelegate.DELEGAGE_PROPERTY_TYPE,
            "Type",
            String.class,
            false);
    addBinding(ProfessionalDelegate.ENTITY_PROPERTY_NAME,
            "Name",
            String.class,
            true);
     ...

实际模型本身就是这样:

public class BindingTableModel<T> extends AbstractTableModel  implements TableModel {

    private List<T> elements;
    private List<BindingTableModelInfo> bindingInfo;

    private Map<Point, Object> valueMap;

    public BindingTableModel(List<T> elements) {
        this.elements = elements;
        this.bindingInfo = new ArrayList<>();
        valueMap = new HashMap<>();
        bind();
        for(int i = 0; i < getRowCount(); i++){
            fireTableRowsInserted(i, i);
        }    
        fireTableStructureChanged();
    }

    public void addBindingInfo(String propertyName, String columnName, Class colClass, boolean isEditable) {
        bindingInfo.add(new BindingTableModelInfo(propertyName,
                columnName,
                colClass,
                isEditable));
    }

    private void bind() {
        for (int col = 0; col < getColumnCount(); col++) {
            for (int row = 0; row < getRowCount(); row++) {
                getValueAt(row, col);//this will init the map
            }
        }
    }

    public void addElement(T element){
        elements.add(element);
    }

    public List<T> getElements(){
        return elements;
    }

    public void removeElement(T element){
        int index = elements.indexOf(element);
        elements.remove(element);
        fireTableRowsDeleted(index, index);
    }

    @Override
    public int getRowCount() {
        return elements.size();
    }

    @Override
    public int getColumnCount() {
        return bindingInfo.size();
    }

    @Override
    public String getColumnName(int i) {
        return bindingInfo.get(i).getColumnName();
    }

    @Override
    public Class<?> getColumnClass(int i) {
        return bindingInfo.get(i).getColumnClass();
    }

    @Override
    public boolean isCellEditable(int row, int col) {
        return bindingInfo.get(col).isIsEditable();

    }

    @Override
    public Object getValueAt(int row, int col) {
        Point point = new Point(row, col);
        if (valueMap.containsKey(point)) {
            return valueMap.get(point);
        }
        String property = bindingInfo.get(col).getProperty();
        Class classType = bindingInfo.get(col).getColumnClass();
        T element = elements.get(row);
        Object retVal = Pojo.getValueByName(classType, element, property);
        valueMap.put(point, retVal);
        return retVal;
    }

    @Override
    public void setValueAt(Object value, int row, int col) {
        Point point = new Point(row, col);
        if (valueMap.containsKey(point)) {
            Object oldValue = valueMap.get(point);
            if (oldValue != null && oldValue.equals(value)) {
                return;
            }
        }
        String property = bindingInfo.get(col).getProperty();
        Class classType = bindingInfo.get(col).getColumnClass();
        T element = elements.get(row);
        Pojo.setValueByName(classType, element, property, value);
    }


}

1 个答案:

答案 0 :(得分:4)

确定fireTableCellUpdated()或其他适当的事件;这通常在setValueAt()

的实施中完成
@Override
public void setValueAt(Object value, int row, int col) {
    …
    fireTableCellUpdated(row, col);
}

完整示例见herehere