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) {
}
((JTextField)component).setText((String)value);
return component;
}
public Object getCellEditorValue() {
return ((JTextField)component).getText();
}
}
Any help would be appreciated. Thank.
source
share