CheckBox节点渲染器和编辑器

时间:2012-01-31 10:16:47

标签: java swing editor jtree treenode

我渲染了一个有两种节点的树。

  1. Leaf Nodes
  2. 父节点
  3. enter image description here

    一个 - 表示父节点。 二,三 - 代表叶节点。

    我是否需要编写两个独立的编辑器? 一个用于父节点,一个用于叶节点?

    我能完成这个吗?

    如何编写新的渲染器来渲染两种不同类型的节点?和他们相应的编辑?

1 个答案:

答案 0 :(得分:1)

只需创建两个单独的TreeCellEditor实现,并将它们与类合并在一起,如下所示:

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);
    }
}