Provide extra behavior when editing a cell in JTable

I am creating a Java application. I need to provide additional behavior when editing a cell in JTable. Ideally, this will happen when the cell loses focus after editing. Depending on some subsequent processing, I might reset the cell value. I tried using the cell editor, but it does not give me the desired behavior.

In JTable, by default, only if I double-click a cell, it will become editable. But in my implementation of CellEditor, the cell becomes editable as soon as it gets into focus.

Here is the code for My custom CellEditor,

public class ParameterDefinitionEditor 
    extends AbstractCellEditor
    implements TableCellEditor{

    private JTable table;
    private DefaultTableModel defaultTableModel;

public ParameterDefinitionEditor(DefaultTableModel defaultTableModel,
JTable table) { 

        super();
        this.table = table;
        this.defaultTableModel = defaultTableModel;

        TableColumnModel columnModel = table.getColumnModel();
        columnModel.getColumn(0).setCellEditor(this);

}

    public Component getTableCellEditorComponent(JTable table, 
                            Object value, 
                         boolean isSelected, 
                        int row, 
                         int column) {

        if (isSelected) {
            // Do some processing.
        } 

        ((JTextField)component).setText((String)value);

        // Return the configured component
        return component;
    }

    public Object getCellEditorValue() {

        return ((JTextField)component).getText();
    }


}

Any help would be appreciated. Thank.

0
source share
3 answers

reset .

, , stopCellEditing().

JTable , . CellEditor .

DefaultCellEditor. setClickCountToStart().

,

, , , TableModelListener TableModel. .

+2

, .

, ( TableModelListener), ​​
tableChanged.

Swing:
http://java.sun.com/docs/books/tutorial/uiswing/components/table.html

+1

, stopCellEditing ( AbstractCellEditor)

public boolean stopCellEditing()
{
String s = (String) getCellEditorValue();
if ( ! valueValidator.isValid(s) )
  {
  editorComponent.setBorder(new LineBorder(Color.red));        
  Toolkit.getDefaultToolkit().beep();
  return false;
  }
}
else { ........
+1

All Articles