Reputation: 28294
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
Upvotes: 2
Views: 4460
Reputation: 546
i fix it a little this solve the problem ; you not allowed to unbind the main bindingGroup but you specify the jtablebinding to update it like this
Binding b = bindingGroup.getBindings().get(0);
b.unbind();
b.bind();
Upvotes: 0
Reputation: 546
Just write this two lines after update or insert
bindingGroup.unbind();
bindingGroup.bind();
Upvotes: 0
Reputation: 28294
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
Upvotes: 1
Reputation: 205785
For reference, the underlying problem is discussed here, and manual solutions are adduced here for both DefaultTableModel
and AbstractTableModel
. In effect, a change to a
or b
must notify any TableModelListener
that c
may have changed. This may help guide your search for a suitable beans binding property.
Upvotes: 1