Reputation: 133
Need some help to help me understand about jtable cell listeners.
My problem is that i need to catch a change in cell, when it catches, i need to get old value and new value.
The reason i'm asking is that i'm using JTable with DefaultTableModel.
I saw other posts about this, but when i'm trying to implement i do not get any "String" results, only serialized results.
Here is what i'm using:
table.addPropertyChangeListener(new PropertyChangeListener() {
@Override
public void propertyChange(PropertyChangeEvent evt) {
System.out.println(evt.getOldValue());
System.out.println(evt.getNewValue());
}
});
this is what i get:
null
javax.swing.JTable$GenericEditor@4b20aa93
javax.swing.JTable$GenericEditor@4b20aa93
null
null
javax.swing.JTable$GenericEditor@4b20aa93
com.apple.laf.AquaImageFactory$SystemColorProxy[r=255,g=255,b=255]
com.apple.laf.AquaImageFactory$SystemColorProxy[r=0,g=0,b=0]
com.apple.laf.AquaImageFactory$SystemColorProxy[r=9,g=80,b=208]
com.apple.laf.AquaImageFactory$SystemColorProxy[r=202,g=202,b=202]
null
false
com.apple.laf.AquaImageFactory$SystemColorProxy[r=0,g=0,b=0]
com.apple.laf.AquaImageFactory$SystemColorProxy[r=255,g=255,b=255]
com.apple.laf.AquaImageFactory$SystemColorProxy[r=202,g=202,b=202]
com.apple.laf.AquaImageFactory$SystemColorProxy[r=9,g=80,b=208]
false
true
com.apple.laf.AquaImageFactory$SystemColorProxy[r=255,g=255,b=255]
com.apple.laf.AquaImageFactory$SystemColorProxy[r=0,g=0,b=0]
com.apple.laf.AquaImageFactory$SystemColorProxy[r=9,g=80,b=208]
com.apple.laf.AquaImageFactory$SystemColorProxy[r=202,g=202,b=202]
true
false
Upvotes: 1
Views: 2585
Reputation: 324098
Two approaches.
The first is to customize the TableModel
and override the setValueAt(...)
method. The basic code would be:
@Override
public void setValueAt(Object newValue, int row, int column)
{
Object oldValue = getValueAt(row, column);
// do processing with your "oldValue" and the "newValue"
super.setValueAt(...);
}
The other approach is to use a "listener" that you can add to any TableModel
. For this approach you can the Table Cell Listener. This class will generate an event whenever the oldValue/newValue has been changed. The event will give you access to both values so you can do your processing.
Depending on your exact requirement you can use either approach.
Upvotes: 1