Editing and Rendering
You can build your own editors and
renderers for the cells in your table. By default, you get renderers
and editors for Boolean
types
(JCheckBox
for display and editing),
ImageIcon
types, Number
types
(right-justified JTextField
for an editor), and
Object
types (JTextField
for
editing). However, you can specify a particular editor or renderer
for a class type or a particular column, or even a particular cell.
The TableCellRenderer Interface
This interface provides access to a rendering component without defining what the component will do. This works because a renderer functions by rubber-stamping a component’s image in a given location. The only method this interface defines initializes and returns just such a component:
- public abstract Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column)
Takes a
value
, which could also be retrieved by getting the cell atrow
,column
oftable
, and returns a component capable of drawing the value in a table cell (or anywhere, really). The resulting drawing can be affected by the selection state of the object and whether or not it currently has the keyboard focus.
The DefaultTableCellRenderer Class
The javax.swing.table
package includes a default renderer that produces a
JLabel
to display text for each cell in the table.
The JTable
class uses this renderer to display
Number
s, Icon
s, and
Object
s. JTable
creates a new default renderer and then aligns ...
Get Java Swing 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.