代码之家  ›  专栏  ›  技术社区  ›  Ervinas34

JTable cell listener JAVA

  •  1
  • Ervinas34  · 技术社区  · 8 年前

    需要一些帮助来帮助我了解jtable cell侦听器。

    我问这个问题的原因是我正在将JTable与DefaultTableModel一起使用。

    以下是我正在使用的内容:

            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
    
    1 回复  |  直到 8 年前
        1
  •  1
  •   camickr    8 年前

    两种方法。

    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(...);
    }
    

    表格模型 Table Cell Listener . 每当oldValue/newValue发生更改时,此类将生成一个事件。该事件将允许您访问这两个值,以便您可以进行处理。

    根据您的具体要求,您可以使用任何一种方法。