![]() |
The Java Developers Almanac 1.4Order this book from Amazon. |
e953. Creating a Custom Table Cell Editor in a JTable ComponentA table cell editor needs to implement theTableCellEditor
interface. This interface supports listeners. The listener code is
conveniently provided by the class AbstractCellEditor so that most
table cell editors extend from this class.
Like a renderer (see e928 Creating a Custom Cell Renderer in a JTable Component), an
editor returns a component used to edit the value in the cell. For
performance reasons, the editor should not create a new component each
time The job of JTable table = new JTable(); // Add some data... // Install the custom editor on the first column int vColIndex = 0; TableColumn col = table.getColumnModel().getColumn(vColIndex); col.setCellEditor(new MyTableCellEditor()); public class MyTableCellEditor extends AbstractCellEditor implements TableCellEditor { // This is the component that will handle the editing of the cell value JComponent component = new JTextField(); // This method is called when a cell value is edited by the user. public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int rowIndex, int vColIndex) { // 'value' is value contained in the cell located at (rowIndex, vColIndex) if (isSelected) { // cell (and perhaps other cells) are selected } // Configure the component with the specified value ((JTextField)component).setText((String)value); // Return the configured component return component; } // This method is called when editing is completed. // It must return the new value to be stored in the cell. public Object getCellEditorValue() { return ((JTextField)component).getText(); } }
e955. Setting the Activation Click Count for a Table Cell Editor in a JTable Component e956. Programmatically Starting and Stopping Cell Editing 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. |