Quick Tip 44: Custom Renderer for OutLineView
In QuickTip 38 and 39 I’ve shown how you can use PropertyEditorSupport to have customized rendering of your components. This is nice, but also has it’s drawbacks. E.g. when rendering your Text, you won’t know wether or not the cell is selected. So you’ll need a renderer instead. For some simple rendering you can simply use a TableCellRenderer, e.g. like this:
public class CustomOutlineCellRenderer extends DefaultOutlineCellRenderer {
@Override
public Component getTableCellRendererComponent(JTable table, Object value,
boolean isSelected, boolean hasFocus, int row,
int column) {
DefaultTableCellRenderer c = null;
if (value instanceof Property) {
Property property = (Property) value;
if (property != null) {
try {
Object propertyValue = property.getValue();
// assuming we have text properties, this will paint OK…
c = (DefaultOutlineCellRenderer) super.getTableCellRendererComponent(
table, propertyValue, isSelected, hasFocus, row, column);
// special handling for images
if (propertyValue instanceof Image) {
c.setText(”");
c.setIcon(ImageUtilities.image2Icon((Image) propertyValue));
return c;
}
} catch (IllegalAccessException ex) {
Exceptions.printStackTrace(ex);
} catch (InvocationTargetException ex) {
Exceptions.printStackTrace(ex);
}
}
} else {
// fallback when no property (shouldn’t happen)
c = (DefaultOutlineCellRenderer) super.getTableCellRendererComponent(
table, value, isSelected, hasFocus, row, column);
}
Outline tbl = (Outline) table;
c.setForeground(tbl.getForeground());
c.setBackground(tbl.getBackground());
if (isSelected) {
c.setBackground(tbl.getSelectionBackground());
c.setForeground(tbl.getSelectionForeground());
}
return c;
}
}
Set in on your OutLineView’s Outline and you’re done:
CustomOutlineCellRenderer cellRenderer = new CustomOutlineCellRenderer();
outLineView.getOutline().setDefaultRenderer(Node.Property.class, cellRenderer);
Now all Properties are displayed like in a simple JTable, without the disabled style painting for readonly properties.


