2011-09-30 49 views
0

我从util.List与beansbinding.I一个JTable插入数据已包裹的ArrayList到Netbeans的一个ObservableList和assinged到uitl.List.I可观察列表绑定数据和设置在“内容表”属性在Netbeans的JTable绑定选项中。第一次更新列表时,JTable也会更新并且没问题。但是,当我把它扔在可观察名单被绑定到JTable列表中的另一util.List第二次,列表更新,但JTable没有更新。(但是当我设置列表中,System.out中。 PR ..打印清单的正确的价值观,在这里我改变了对util.List和ObservableList反之亦然找到问题的所在,但没有结果如我所料)(但是当我添加对象绑定到JTable中,则列表JTable中已更新。) 如何更新JTable中时,列表被更新(这时候我设置一个新的列表意味着,该表也被更新一次,我设置一个新的列表时)。的JTable beansbinding

下面是用我的代码来设置列表

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(); 
    } 
} 
+0

该代码片段看起来不错 - 我能想到可能会出错的是PROP_常量值的拼写... – kleopatra

+0

我认为这个代码是可以的,因为第一次设置列表是ok.JTable获取update.the第二次列表更新不起作用。另外,当我将一个对象添加到绑定到JTable的列表中时,JTable将获取更新。 'suggestionList.add(myObject);'然后添加的对象显示在JTable中,并且可以添加任意数量的ojbects。任何方式感谢您的关注 –

回答

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代码:该getter/setter方法签名应该具有相同的类型,你不。在我的测试中没有什么不同,在你的上下文中可能会或可能不会指示一些不必要的混淆