简体   繁体   中英

Problems binding JTable selectedRow to JTextField (org.jdesktop.beansbinding)

I have a Java Swing application and want to bind the selected row of a JTable to a JTextField. My binding looks as follows:

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

The text field is filled one time at the beginning with '-1', because no row is selected. If I click on a row, there is no update of the text field. One ugly workaround is to call the unbind() and bind() method in the mouse listener of the table. But I think there is something missing during my binding.

Maybe one of you has an idea. Thanks!

The documentation says, that "selectedElement" can be used for this purpose. With this property, it works without the ugly unbind() and bind() .

Now the code looks as follows:

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

To convert "MyObject" to a "String", I added a converter to the binding.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM