1
votes

I have rendered a tree with two kind of nodes .

  1. Leaf Nodes
  2. Parent Nodes

enter image description here

One - represents a parent node . Two, Three - Represents leaf nodes .

And do i need to write two separate editors ? One for the parent nodes and one for the leaf nodes ?

Hoa can i accomplish this ?

How do i write a new renderer for rendering two different kind of nodes ? And the corresponding Editor for them ?

1

1 Answers

1
votes

Just create two separate TreeCellEditor implementation and merge them together with the class like this:

public class TreeCellEditorDelegate extends DefaultTreeCellEditor {

    private final TreeCellEditor    editorParent;
    private final TreeCellEditor    editorLeaf;

    public TreeCellEditorDelegate(
            final JTree tree,
            final DefaultTreeCellRenderer renderer,
            final TreeCellEditor editorParent,
            final TreeCellEditor editorLeaf) {
        super(tree, renderer);
        this.editorParent = editorParent;
        this.editorLeaf = editorLeaf;
    }

    @Override
    public Component getTreeCellEditorComponent(
            final JTree tree,
            final Object value,
            final boolean isSelected,
            final boolean expanded,
            final boolean leaf,
            final int row) {
        if (leaf)
            return editorLeaf.getTreeCellEditorComponent(tree, value, isSelected, expanded, true, row);
        else
            return editorParent.getTreeCellEditorComponent(tree, value, isSelected, expanded, false, row);
    }
}