tags:

views:

117

answers:

3

Hi!

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

+4  A: 

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?

Carl Manaster
Thank you very much for the suggestion! This solved my problem! I'm now implementing the TableCellRenderer interface and inspecting the type, forwarding to the desired CellRenderer.
John Smith
+2  A: 
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.

z5h
These would set the default editor/renderer for types on the table as a whole. I think the goal is to set them for a column or a set of columns only.
akf
+1  A: 

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.

trashgod