X

Geertjan's Blog

  • October 7, 2007

Tweaking a TreeTableView

Geertjan Wielenga
Product Manager
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.

Join the discussion

Comments ( 5 )
  • hilz Sunday, October 7, 2007

    I thought i posted this before but i don't see my post. i am posting again. So my question is, in this example, you have one kind of node (FileNode) that represends a directory and a file. What if i have more than one kind of nodes in my tree, and i want to show different columns for different kinds of nodes. is that possible in a TreeTableView ?


  • Balazs Monday, October 8, 2007

    A very big thanks for this post.

    It took me a while (and reading your code :-) ) that the only possible way of setting the tree column's name is to call setName on the root node.

    Is there any way of making the tree column sortable?

    I have the following code intended for the tree column:

    public class MainDescriptionProperty extends PropertySupport.ReadOnly

    {

    private final String description;

    public MainDescriptionProperty( String description )

    {

    super( "main_description", // property name

    String.class, // property type

    "Description", // header name -> seems that it is ignored if this property belongs to the tree column

    "Description tooltip" // header tooltip

    );

    this.description = description;

    this.setValue( "TreeColumnTTV", Boolean.TRUE ); // main tree column

    this.setValue( "ComparableColumnTTV", Boolean.TRUE ); // sortable

    this.setValue( "SortingColumnTTV", Boolean.FALSE ); // initially not sorted

    }

    public Object getValue() throws IllegalAccessException, InvocationTargetException

    {

    return description;

    }

    public PropertyEditor getPropertyEditor()

    {

    return null;

    }

    }

    It puts the sorting arrow in the header, but does not do anything. Is there any way to apply sorting for this column, too?


  • Geertjan Monday, October 8, 2007

    hilz, hope my blog today answers at least part of your question.

    Balazs, hi! How are you assigning the above propert support to the tree column? I mean how/where are you instantiating it?


  • Geertjan Monday, October 8, 2007

    Balazs, I tried to show how to make the tree column sortable, in the next blog entry.


  • Joseph Wednesday, August 18, 2010

    I'm using a PropertyEditorSupport that supports a customSupportEditor. On my custom editor, the OK button behaves like d CANCEL button and vice-versa. This is driving me crazy. Is there a solution for this.


Please enter your name.Please provide a valid email address.Please enter a comment.CAPTCHA challenge response provided was incorrect. Please try again.