如何在Swing JTree中禁用扩展符号? [英] How to disable expand sign in Swing JTree?

查看:100
本文介绍了如何在Swing JTree中禁用扩展符号?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在Swing中工作,我想在某些类型的节点上禁用扩展(加[+])符号.

I'm working in Swing and I would like to disable the expand (plus [+]) sign on a certain type of nodes.

不确定如何执行此操作,因为我的节点没有离开,而且我也不能使用setShowsRootHandles(仅用于根).

Not sure how to do it because my nodes aren't leaves and I also cannot use setShowsRootHandles (which is only for the root).

我指的是JTree:假设我有这个结构:

I'm referring to to JTree: suppose i got this structure:

-[+] node1

--[+] node1

-[+] node2

--[+] node2

当我加载此结构时,我不想在node2上看到[+]符号(因为它是一个特殊类型的节点).但是我也想通过使用特殊命令来扩展它.

when I load this structure i would like not to see the [+] sign on node2 (because it a special type node). But I also would like to expand it by using a special command.

我已经覆盖了isLeaf()(来自DefaultMutableTreeNode的方法),所以当我位于特殊类型节点中时,它将设置为TRUE,但是当我尝试扩展它时,它将不会扩展,因为isLeaf ()== TRUE ...

I've overridden isLeaf() (method from DefaultMutableTreeNode) so it would set to to TRUE when i'm in the special type node, but then when I'm trying to expand it, it wouldn't expand because isLeaf() == TRUE...

希望这会让事情变得更清楚.

Hope this will make things more clear.

推荐答案

虽然无法删除句柄,但可以限制节点的扩展.可行的方法是将TreeWillExpandListener与具有状态限制扩展的自定义treeNode结合使用:

While it is not possible to remove the handles, it is possible to restrict the expansion of nodes. The way to go is a TreeWillExpandListener combined with a custom treeNode that has state to restrict expansion:

  • 下面的自定义节点具有一个可扩展属性,默认情况下为false
  • 检测到自定义节点时,侦听器会基于该可扩展属性允许/否决扩展
  • 对于程序扩展,可扩展属性会暂时设置为true以通过侦听器

示例代码:

// mixed tree of normal/restricted noded
DefaultMutableTreeNode root = new DefaultMutableTreeNode("root");
DefaultMutableTreeNode normalSubTree = new DefaultMutableTreeNode("normal");
normalSubTree.add(new DefaultMutableTreeNode("normalChild"));
MyNode restrictedSubTree = new MyNode("restrictedSubtree");
restrictedSubTree.add(new DefaultMutableTreeNode("restrictedChild"));
root.add(normalSubTree);
root.add(restrictedSubTree);
final JTree tree = new JTree(root);
// the listener which vetos expansion of MyNodes that are not expandable
TreeWillExpandListener l = new TreeWillExpandListener() {

    @Override
    public void treeWillExpand(TreeExpansionEvent event)
            throws ExpandVetoException {
        TreePath path = event.getPath();
        if (path.getLastPathComponent() instanceof MyNode) {
            if (!((MyNode) path.getLastPathComponent()).isExpandable()) {
                throw new ExpandVetoException(event, "node not expandable");
            }
        }
    }

    @Override
    public void treeWillCollapse(TreeExpansionEvent event)
            throws ExpandVetoException {
    }
};
tree.addTreeWillExpandListener(l);

Action expand = new AbstractAction("Expand") {

    @Override
    public void actionPerformed(ActionEvent e) {
        TreePath selected = tree.getSelectionPath();
        if (selected == null) return;
        if (selected.getLastPathComponent() instanceof MyNode) {
            MyNode last = (MyNode) selected.getLastPathComponent();
            boolean old = last.isExpandable();
            last.setExpandable(true);
            tree.expandPath(selected);
            last.setExpandable(old);
        }
    }
};

    JXFrame frame = wrapWithScrollingInFrame(tree, "veto expand");
    addAction(frame, expand);
    show(frame);
}

// custom node which has an expandable property
public static class MyNode extends DefaultMutableTreeNode {

    private boolean expandable;

    public MyNode() {
        this(null);
    }

    public MyNode(Object userObject) {
        super(userObject);
    }

    public void setExpandable(boolean expandable) {
        this.expandable = expandable;
    }

    public boolean isExpandable() {
        return expandable;
    }
}

这篇关于如何在Swing JTree中禁用扩展符号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆