javaswinguser-interfacebindingbeans-binding

Refresh JTable after Update


Here is a simple example of the problem I am running into.

I have a simple class:

public class Test{
    Integer a;
    Integer b;

    //getters and setters

    public void getC()
    {
        return a + b;
    }

}

Note that it has a property called C which is the sum of a and b.

I then bind this to a JTable like so:

List<Test> testList = new ArrayList<Test>();
...add some Test objects to the list
ObservableList observableList = ObservableCollections.observableList(testList);
JTable table = new JTable();
JTableBinding tableBinding = SwingBindings.createJTableBinding(AutoBinding.UpdateStrategy.READ_WRITE, observableList, table); 

I then use the following code to add the bindings for each property a, b, and c of the test object. (Note this is just the generic code I use)

BeanProperty beanProperty = BeanProperty.create(properyName);
JTableBinding.ColumnBinding columnBinding = tableBinding.addColumnBinding(beanProperty);
columnBinding.setColumnName(columnName);
columnBinding.setColumnClass(clazz);
columnBinding.setEditable(editable);

Now this will correctly display the table, but the problem happens when I update either a or b in the table. Since c is calculated off a and b, I expect c to update when one of these values changes. This does not happen.

I guess the table needs refreshed to reflect the new value of the entities?

Can anyone explain what I need to do to make this happen? Do I need to add some aditional beans binding property?

Here is the beans binding library I am using:

<dependency>
  <groupId>org.jdesktop</groupId>
  <artifactId>beansbinding</artifactId>
  <version>1.2.1</version>
</dependency>


org.jdesktop.swingbinding.SwingBindings

Solution

  • With help from this question here, I was able to get the required functionality by overriding the setVale function on my JTable to:

    @Override
    public void setValueAt(Object value, int row, int col)
    {
        super.setValueAt(value, row, col);
        tableBinding.unbind();
        tableBinding.bind();
        revalidate();
    }
    

    Thanks for the leads trashgod