开发者

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 i开发者_Go百科s 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...


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?


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜