复选框节点渲染器和编辑器

发布于 2024-12-31 23:23:02 字数 263 浏览 2 评论 0原文

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

  1. 叶节点
  2. 父节点

在此处输入图像描述

1 - 代表父节点。 二、三——代表叶节点。

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

我可以完成这个吗?

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

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 ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

烟雨扶苏 2025-01-07 23:23:02

只需创建两个单独的 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);
    }
}

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);
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文