SOURCECODE |
How to... define a TableCellEditor
Description:
A TableCellEditor is needed to define the Component that displays the entry of a cell in a JTable, to check wether an edited value is valid and to inform it's parent table that an editing event finished.
Our CellEditor will have a util.swing.JTextInput as the Component to display and edit the entry.
The validity of a initial cell value is being tested in public Object getCellEditorValue()
by simply trying to parse it to a Currency. If parsing fails, zero will be returned and thereby no wrong value may be put into the cell and cause any trouble.
The validity of an edited cell value is being checked in public boolean stopCellEditing
, which informs the calling member that a correct value has been entered. It checks the value just like getCellEditorValue() by trying to parse it. If it fails, there will be an error message and editing cannot be stoped until a valid value is being entered or editing is being aborted by pressing the escape key.
ToDo's:
- Extend the DefaultCellEditor
- Initialize the attributes in the constructor
- Define how to get the CellEditors Component and its initial value to display and check its validity. In our case we return a util.swing.JTextInput for reasons of serializability with the initial value being checked by trying to parse it. If that fails, zero will be returned.
- Define how to resolve the value of the cell. Needed in case of table or window updating.
- Define how to indicate the successful attempt of editing a cell. Here we, too, try to parse the entered value, but do not return zero if it fails. There will be an error message instead and the return of false, which prevents the editing to stop until a valid value is being entered or it is being aborted.
Uses:
JTextInput
import data.*;
import sale.*;
import util.swing.*;
import java.awt.*;
import java.text.*;
import javax.swing.*;
1
//extend the DefaultCellEditor
public class DMCellEditor extends DefaultCellEditor {
//this is needed for the JTextInput
private String[] result;
2
//initialize the attributes
public DMCellEditor (String[] result, String init) {
super(new JTextInput(result, init));
this.result = result;
}
3
//define the Component and the value it shall display at first
public Component getTableCellEditorComponent( JTable jTable,
Object value,
boolean isSelected,
int row,
int column) {
//fetch the component and the initial value
Component component = super.getTableCellEditorComponent( jTable,
value,
isSelected,
row,
column);
//initialize the JTextInput, which is needed for editing
//and used because it is serializable
((JTextInput)component).setText(((Currency)
Shop.getTheShop().getCatalog("DM")).toString((NumberValue)value));
//and return it
return component;
}
4
public Object getCellEditorValue() {
//parse the value of the JTextInput
try {
return ((Currency)Shop.getTheShop().getCatalog("DM")).parse(result[0]);
}
catch (ParseException pexc) {
//return zero if it fails
return new IntegerValue (0);
}
}
5
//indicate wether an editing was successful or not
public boolean stopCellEditing() {
try {
((Currency)Shop.getTheShop().getCatalog("DM")).parse(result [0]);
}
catch (ParseException pexc) {
//create an error message if parsing failed
JOptionPane.showMessageDialog(null, "Incorrect value entered, has to be \" DM\"");
//and return false to stay in editing mode
return false;
}
return super.stopCellEditing();
}
}