Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JTable cell value change listener

Which listener can be used to react to the event of JTable cell value change? I tried using TableModelListener but either I got something wrong, or this listener doesn't react on changing the contents of a cell.

I need to act either to changing the content of a cell or to cell losing focus. What is the best way to do this? Thanks.

like image 793
celicni Avatar asked Sep 30 '11 00:09

celicni


1 Answers

class extends JTable {

.... (inside contructor)

addPropertyChangeListener(new PropertyChangeListener() {

    @Override
    public void propertyChange(PropertyChangeEvent evt) {
        if ("tableCellEditor".equals(evt.getPropertyName())) {
            if (isEditing())
                processEditingStarted();
            else
                processEditingStopped();
        }
    }
});

... (end constructor)

protected void processEditingStopped() {
    System.out.println("save " + editingRow + ":" + editingColumn);
}

protected void processEditingStarted() {
    System.out.println("edit " + editingRow + ":" + editingColumn);
    if (editRow > -1 && editColumn > -1)
        oldValue = (String) model.getValueAt(editRow, editColumn);
}

}

more details here: http://tips4java.wordpress.com/2009/06/07/table-cell-listener/

like image 101
ungalcrys Avatar answered Sep 29 '22 07:09

ungalcrys