jTable beansbinding

问题描述 投票:0回答:1

我将util.List中的数据插入到带有beansbinding的JTable中。我将一个ArrayList包装到一个ObservableList和Observable列表中,该列表被绑定到Netbeans中的uitl.List.I绑定数据,并在Netbeans的JTable Beanbinding中的'Table Content'中设置属性选项。第一次更新列表时,JTable也会更新,没关系。但是第二次当我将另一个被嵌入Observable列表的util.List添加到绑定到JTable的列表时,列表会更新,但JTable不会更新。(但是当我设置一个列表时,System.out。 pr ..打印列表的正确值,这里我将util.List更改为ObservableList,反之亦然,找到问题所在,但没有结果如我所料)(但是当我将对象添加到绑定到JTable的列表中时, JTable已更新。)如何更新列表时更新JTable(这意味着当我设置新列表时,每次设置新列表时表也会更新)。

这是我用来设置List的代码

 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();
    }
}
java swing desktop-application beans-binding
1个回答
2
投票

刚检查:它按预期工作(手动编码当然不会触及Netbeans),sourceBean具有属性suggestionList的类;

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

摘要:你没有显示的代码有问题;-)

BTW:getters / setters签名应该具有相同的类型,而不是你的。在我的测试中没有任何区别,在你的上下文中可能会或可能不会表示一些不必要的混淆

© www.soinside.com 2019 - 2024. All rights reserved.