JTable 单元格侦听器 JAVA
JTable cell listener JAVA
需要一些帮助来帮助我理解 jtable 单元监听器。
我的问题是我需要捕获单元格中的变化,当它捕获时,我需要获取旧值和新值。
我问的原因是我将 JTable 与 DefaultTableModel 一起使用。
我看到了关于此的其他帖子,但是当我尝试实施时,我没有得到任何 "String" 结果,只有序列化结果。
这是我正在使用的:
table.addPropertyChangeListener(new PropertyChangeListener() {
@Override
public void propertyChange(PropertyChangeEvent evt) {
System.out.println(evt.getOldValue());
System.out.println(evt.getNewValue());
}
});
这是我得到的:
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
两种方法。
首先是自定义TableModel
,覆盖setValueAt(...)
方法。基本代码为:
@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(...);
}
另一种方法是使用可以添加到任何 TableModel
的 "listener"。对于这种方法,您可以 Table Cell Listener。每当 oldValue/newValue 被更改时,此 class 将生成一个事件。该事件将使您能够访问这两个值,以便您进行处理。
根据您的具体要求,您可以使用任何一种方法。
需要一些帮助来帮助我理解 jtable 单元监听器。
我的问题是我需要捕获单元格中的变化,当它捕获时,我需要获取旧值和新值。
我问的原因是我将 JTable 与 DefaultTableModel 一起使用。
我看到了关于此的其他帖子,但是当我尝试实施时,我没有得到任何 "String" 结果,只有序列化结果。
这是我正在使用的:
table.addPropertyChangeListener(new PropertyChangeListener() {
@Override
public void propertyChange(PropertyChangeEvent evt) {
System.out.println(evt.getOldValue());
System.out.println(evt.getNewValue());
}
});
这是我得到的:
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
两种方法。
首先是自定义TableModel
,覆盖setValueAt(...)
方法。基本代码为:
@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(...);
}
另一种方法是使用可以添加到任何 TableModel
的 "listener"。对于这种方法,您可以 Table Cell Listener。每当 oldValue/newValue 被更改时,此 class 将生成一个事件。该事件将使您能够访问这两个值,以便您进行处理。
根据您的具体要求,您可以使用任何一种方法。