TreeTableRender for JXTreeTable

686 Views Asked by At

How can I assign different Renderers for different Column in a JXTreeTable. The JXtreeTable has different Column Types (like currency, date,...etc.). In a JXTable I used the getColumn(identifier).setCellRender(TableCellRender) Method. What should I do for a JXTreeTable?

1

There are 1 best solutions below

0
On

As long as all columns of the same type are to be handled equally, you can just assign a default renderer by class. For instance, your table has two columns with java.util.Date values and you want them both handled in the same way:

myTreeTable.setDefaultRenderer(java.util.Date.class, new DefaultTableRenderer(StringValues.DATE_TO_STRING));

First argument is the type of object you want this renderer to handle. Second argument is the cell renderer. SwingX comes with a very simple rendering mechanism called DefaultTableRenderer. It takes a number of possible arguments, but the simplest one is to provide a StringValue instance. That's an interface that, when implemented, takes an arbitrary Object and converts it into a String in some manner. The StringValues class contains a few predefined implementations. In the example above, I use StringValues.DATE_TO_STRING, which passes the java.util.Date object in the cell to a default DateFormat instance and returns the String result. Go on like this for all classes of object you want to handle.

If you need specific rendering for a single column only, you just plug the column into the middle of the call:

myTreeTable.getColumnExt(myColumnIdentifier).setCellRenderer(java.util.Date.class, new DefaultTableRenderer(myGloriousStringValueConverter));

Note that the call is no longer to setDefaultCellRenderer.

This Rendering mechanism is one of the best features of SwingX. Easy to use and quite potent in combination with the Highlighter pipeline.

All this applies to the table portion of the JXTreeTable. The hierarchical column (first column) can be setup in the same way, only that the call is

myTreeTable.setTreeCellRenderer(new DefaultTreeRenderer(myGloriousStringValue));