John Smith
John Smith

Reputation: 31

Java Swing: Different TableCellRenderers

Given a javax.swing.table.TableColumn... is it possible in some way to specify the TableCellRenderer/TableCellEditor to be used for a given type rather than having the TableColumn use the same TableCellRenderer/TableCellEditor?

I'm aware that I can specify some DefaultCellEditor/Renderer in the JTable, but this is not what I want.

Due to details (legacy code specifics), I am not able to override the JTable#getCellEditor(int,int) and JTable#getCellRenderer(int,int).

Appreciate any suggestions...

Upvotes: 0

Views: 447

Answers (3)

trashgod
trashgod

Reputation: 205825

Note that setDefaultRenderer() and setDefaultEditor() each specify a particular class for which they should be invoked. It's fairly easy to design a composite type with a custom renderer and editor, as suggested in this example. The data model should return that custom type for a given column, but the renderer and editor are free to interpret such values arbitrarily based on content or row. In the example, Value is modeled as both a Boolean and a Double. The corresponding view uses a check box and a formatted decimal string, while Value's compareTo() method ensures numeric sorting.

Upvotes: 2

Mark Bolusmjak
Mark Bolusmjak

Reputation: 24409

public void setDefaultEditor(java.lang.Class<?> columnClass,
                             javax.swing.table.TableCellEditor editor)

public void setDefaultRenderer(java.lang.Class<?> columnClass,
                               javax.swing.table.TableCellRenderer renderer)

Or do as Carl said. Your single editor renderer looks at the value it got back and delegates to some other renderers/editors.

Upvotes: 2

Carl Manaster
Carl Manaster

Reputation: 40356

I'm not sufficiently familiar with TableCellRenderer to be sure this is appropriate, but could you not specify one that looks at the content, then dispatches to other renderers based on the type?

Upvotes: 4

Related Questions