user3297735
user3297735

Reputation: 11

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!

Upvotes: 0

Views: 718

Answers (1)

user3297735
user3297735

Reputation: 11

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.

Upvotes: 0

Related Questions