Sunday October 07, 2007
Tweaking a TreeTableView
There's an interesting question at the end of yesterday's blog entry, by Kees Kuip, from the JMeld project, about text color and row color in org.openide.explorer.view.TreeTableView. How to set them? Well, you can create your own PropertyEditorSupport, as indicated below. The highlighted method is all I added to yesterday's code:
private class FullPathProperty extends PropertySupport.ReadOnly<String> {
File file;
public FullPathProperty(File file) {
super(FileNode.PROP_FULL_PATH, String.class, "Full path", "Complete path is shown");
this.file = file;
}
@Override
public PropertyEditor getPropertyEditor() {
return new FullPathPropertyEditor();
}
public String getValue() throws IllegalAccessException, InvocationTargetException {
return file.getAbsolutePath();
}
}
Then, you need to extend good old java.beans.PropertyEditorSupport. No NetBeans API magic at all. For example, now I have made the text in the "Full path" column red:
private class FullPathPropertyEditor extends PropertyEditorSupport {
@Override
public String getAsText() {
return (String) getValue();
}
@Override
public void paintValue(Graphics g, Rectangle box) {
g.setColor(Color.red);
g.drawString(getAsText(), box.x + 5, box.y + 15);
}
@Override
public boolean isPaintable() {
return true;
}
}
And this is the result:
You can use the paintValue method in many other ways, also to color the background of the cell. But that's all standard java.beans.PropertyEditorSupport stuff. To color alternate cells in the column, you could put an iterating number into a map whenever a new node is created. Then cycle through the map and make every odd numbered cell a distinct color. Probably you'd use isPaintable to determine whether a cell should be painted. You'd apply the same to the cells of the other columns in your table, as below:
Or some other similar solution. Also see Tim Boudreau's NetBeans Property Editor Tutorial for some interesting insights.
Oct 07 2007, 12:04:50 PM PDT Permalink


