我使用bound绑定将数据从util.List插入到JTable中。我已经将ArrayList包装到ObservableList中,并将观察列表赋值给uitl.List.我在Netbeans中绑定了数据,并在Netbeans的“JTable bound绑定选项”中的“表内容”中设置了属性。第一次更新列表时,JTable也会更新,并且没有问题。但是当我第二次将另一个转换为可观察列表的util.List设置为绑定到该JTable的列表时,该列表会更新,但JTable不会更新。(但当我设置一个列表时,System.out.pr..打印列表的正确值,这里我将util.List更改为ObservableList,反之亦然,以查找问题所在,但没有得到我预期的结果)(但当我向绑定到JTable的列表添加对象时,JTable被更新。)如何在更新列表时更新JTable (这意味着当我设置新列表时,该表也会在每次设置新列表时更新)。
下面是我用来设置列表的代码
public List<Customer> getSuggestionList() {
return suggestionList;
}
public void setSuggestionList(ObservableList suggestionList) {
try {
List oldSuggestionList = this.suggestionList;
this.suggestionList = suggestionList;
propertySupport.firePropertyChange(PROP_SUGGESTIONLIST, oldSuggestionList, suggestionList);
System.out.println("Suggestionlist is setted-----------");
Customer c = (Customer) suggestionList.get(0);
System.out.println("sugesstion list customer--------" + c.getCustFname());
} catch (Exception e) {
e.printStackTrace();
}
}发布于 2011-09-30 16:37:11
刚刚检查过:它工作正常(当然是手工编码,不会接触Netbeans ),对具有suggestionList属性的类进行sourceBean;
BindingGroup context = new BindingGroup();
BeanProperty valuesProperty = BeanProperty.create("suggestionList");
JTableBinding tableBinding = SwingBindings.createJTableBinding(
UpdateStrategy.READ_WRITE,
sourceBean, valuesProperty,
table);
context.addBinding(tableBinding);
tableBinding.addColumnBinding(BeanProperty.create("firstName"));
tableBinding.addColumnBinding(BeanProperty.create("lastName"));
context.bind();
// add a button which changes the suggestionList
Action next = new AbstractAction("new data") {
public void actionPerformed(ActionEvent e) {
sourceBean.setSuggestionList(createRandomData());
}
};
button.setAction(next);摘要:您未显示的代码有问题;-)
顺便说一下: getters/setters签名应该具有相同的类型,而您的签名不应该具有相同的类型。在我的测试中没有什么不同,在您的上下文中可能会也可能不会出现一些不想要的混淆
https://stackoverflow.com/questions/7606570
复制相似问题