2014-02-11 24 views
0

我有一個Java Swing應用程序,並希望將選定的JTable行綁定到JTextField。我的綁定看起來如下:將JTable selectedRow綁定到JTextField的問題(org.jdesktop.beansbinding)

BeanProperty<JTable, Integer> tableBeanProperty = BeanProperty.create("selectedRow"); 
    BeanProperty<JTextField, String> textFieldProperty = BeanProperty.create("text"); 
    Binding<JTable, Integer, JTextField, String> binding = Bindings.createAutoBinding(UpdateStrategy.READ_WRITE, table1, tableBeanProperty, field1, textFieldProperty); 
    binding.bind(); 

該文本字段在'-1'的開頭填充一次,因爲沒有選中行。如果我點擊一行,則不會更新文本字段。 一個醜陋的解決方法是在表的鼠標偵聽器中調用unbind()bind()方法。但我認爲在我的裝訂過程中缺少一些東西。

也許你們中的一個人有一個想法。 謝謝!

回答

0

該文件說,「selectedElement」可用於此目的。有了這個屬性,它沒有醜陋的unbind()bind()

現在的代碼如下:

BeanProperty<JTable, MyObject> tableBeanProperty = BeanProperty.create("selectedElement"); 
BeanProperty<JTextField, String> textFieldProperty = BeanProperty.create("text"); 
Binding<JTable, MyObject, JTextField, String> binding = Bindings.createAutoBinding(UpdateStrategy.READ_WRITE, table1, tableBeanProperty, field1, textFieldProperty); 
binding.bind(); 

要「MyObject的」轉換爲「字符串」,我添加了一個轉換器的結合。