我使用绑定的JTable来显示实体列表。
// selSteps is a List of entities.
selStepsBound = ObservableCollections.observableList(selSteps);
JTableBinding jTableBinding = SwingBindings.createJTableBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, selStepsBound, tblSelSteps, "tblSelStepsBinding");
ColumnBinding columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${steporder}"));
columnBinding.setColumnName("Order");
columnBinding.setColumnClass(Integer.class);
columnBinding.setEditable(false);
columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${name}"));
columnBinding.setColumnName("Description");
columnBinding.setColumnClass(String.class);
bindingGroup.addBinding(jTableBinding);
jTableBinding.bind();
bindingGroup.bind();
这适用于显示原始数据。我甚至可以从绑定的observableList中删除项目,并自动更新JTable UI。
问题是当我更改实体的属性值时,该值不会反映在JTable UI中。
如何通知JTable有关更改?
答案 0 :(得分:0)
我对SwingBindings的东西并不是很熟悉,但看起来虽然你的List是Observable(即听众会收到更改的通知),但你的实体却没有。所以没有人听你的实体的变化。
我通常通知JTable其中一个实体的更改方式是从表的模型中触发TableModelEvent。这就是JTable正在监听的更新内容。我通常从ABstractTableModel扩展,以便我可以调用像fireTableCellUpdated(row,col)等好的函数。这个策略如何与SwingBindings混合,我不太确定...
答案 1 :(得分:0)
您需要添加"属性更改支持"到你的实体。所以添加一个成员变量......
final private PropertyChangeSupport changeSupport = new PropertyChangeSupport(this);
和方法......
public void addPropertyChangeListener(PropertyChangeListener listener) {
changeSupport.addPropertyChangeListener(listener);
}
public void removePropertyChangeListener(PropertyChangeListener listener) {
changeSupport.removePropertyChangeListener(listener);
}
然后在每个setter中添加一条类似于此的行......
changeSupport.firePropertyChange("myProperty", oldMyProperty, myProperty);
e.g。 public void setTotalWeight(Integer totalWeight){
Integer oldTotalWeight = this.totalWeight;
this.totalWeight = totalWeight;
changeSupport.firePropertyChange("totalWeight", oldTotalWeight, totalWeight);
}