![]() |
The Java Developers Almanac 1.4 |
|
e956. Programmatically Starting and Stopping Cell Editing in a JTable ComponentNormally, the table component automatically starts and stops the editing of table cells based on user input. However, when building table commands, it may be necessary to programmatically enable and disable editing of cells. The following code demonstrates how to set a cell in edit mode: // Create table
int rows = 10;
int cols = 5;
JTable table = new JTable(rows, cols);
// Enable the ability to select a single cell
table.setColumnSelectionAllowed(true);
table.setRowSelectionAllowed(true);
// Set the cell on the 2nd row, 4th column in edit mode
int row = 1;
int col = 3;
boolean success = table.editCellAt(row, col);
if (success) {
// Select cell
boolean toggle = false;
boolean extend = false;
table.changeSelection(row, col, toggle, extend);
} else {
// Cell could not be edited
}
The following code saves the current value in the cell
being edited and stops the editing process:
if (table.getCellEditor() != null) {
table.getCellEditor().stopCellEditing();
}
The following code discards any changes made by the user
and stops the editing process:
if (table.getCellEditor() != null) {
table.getCellEditor().cancelCellEditing();
}
e954. Preventing Invalid Values in a Cell in a JTable Component e955. Setting the Activation Click Count for a Table Cell Editor in a JTable Component e957. Creating a Text Field That Mirrors the Value in the Anchor Cell in a JTable Component e958. Disabling User Edits in a JTable Component e959. Using a JComboBox in a Cell in a JTable Component e960. Using a List JSpinner as a Cell Editor in a JTable Component
© 2002 Addison-Wesley. |