Editing Cells

In addition to custom renderers, you can also create custom editors for your table cells. (Actually, the basic stuff in this section also applies to the JTree class.) You have several options ranging from straightforward to completely homegrown.

The CellEditor Interface

This interface governs the basic functionality required of an editor. It has methods for retrieving a new value and determining when to start and stop editing. The basic process for editing is:

  • The user clicks the required number of times on the cell (varies from editor to editor).

  • The component (usually JTree or JTable) replaces the cell with its editor.

  • The user types or chooses a new value.

  • The user ends the editing session (e.g., pressing Enter in a text field).

  • The editor fires a change event to interested listeners (usually the tree or table containing the cell), stating that editing is finished.

  • The component reads the new value and replaces the editor with the cell’s renderer.

Events

The CellEditor interface requires methods for adding and removing cell editor listeners, which are objects interested in finding out whether editing is finished or canceled. The CellEditorListener class is discussed later in the chapter.

public abstract void addCellEditorListener(CellEditorListener l)
public abstract void removeCellEditorListener(CellEditorListener l)

Methods

public Object getCellEditorValue( )

Access the only property of a cell editor, which is the cell’s current value. After successful editing, a table or ...

Get Java Swing, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.