O'Reilly logo

Java Swing, 2nd Edition by Brian Cole, James Elliott, Marc Loy, Robert Eckstein, Dave Wood

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required