The Java Developers Almanac 1.4


Order this book from Amazon.

   
Home > List of Packages > javax.swing.table  [62 examples] > Editing  [8 examples]

e953. Creating a Custom Table Cell Editor in a JTable Component

A table cell editor needs to implement the TableCellEditor 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 getTableCellEditorComponent() is called. Rather, it should return the same component (or one from a set) every time.

The job of getTableCellEditorComponent() is to configure the component based on the coordinates and value in the cell. The table then uses the configured component and paints it on the screen. After painting it, the table no longer needs the component.

    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();
        }
    }

 Related Examples
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
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

See also: Cells    Column Heads    Columns    Events    Layout    Rows    Scrolling    Selection    Sorting    Table Model    Tool Tips   


© 2002 Addison-Wesley.