简体   繁体   English

如何在Swing JTree中禁用展开登录?

[英]How to disable expand sign in Swing JTree?

I'm working in Swing and I would like to disable the expand (plus [+]) sign on a certain type of nodes. 我在Swing工作,我想在某种类型的节点上禁用扩展(加[+])符号。

Not sure how to do it because my nodes aren't leaves and I also cannot use setShowsRootHandles (which is only for the root). 不知道怎么做,因为我的节点不是叶子,我也不能使用setShowsRootHandles (仅用于root)。

I'm referring to to JTree: suppose i got this structure: 我指的是JTree:假设我得到了这个结构:

Root

--[+] node1 - [+] node1

--[+] node2 - [+] node2

when I load this structure i would like not to see the [+] sign on node2 (because it a special type node). 当我加载这个结构时,我不想在node2上看到[+]符号(因为它是一个特殊的类型节点)。 But I also would like to expand it by using a special command. 但我也想通过使用特殊命令来扩展它。

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... 我重写了isLeaf()(来自DefaultMutableTreeNode的方法),所以当我在特殊类型节点时它会设置为TRUE,但是当我试图扩展它时,它不会扩展,因为isLeaf()= = TRUE ......

Hope this will make things more clear. 希望这会使事情变得更加清晰。

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: 要走的路是TreeWillExpandListener与自定义treeNode结合使用,该树状态具有限制扩展的状态:

  • the custom node below has an expandable property that's false by default 下面的自定义节点具有可扩展属性,默认情况下为false
  • when detecting custom nodes, the listener allows/vetoes expansion based on that expandable property 在检测自定义节点时,侦听器允许/否决基于该可扩展属性的扩展
  • for programmatic expansion, the expandable property is set to true temporarily to pass the listener 对于程序化扩展,可扩展属性暂时设置为true以传递侦听器

Example code: 示例代码:

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

It's possible to remove the handles - despite what others have mentioned. 除非其他人提到过,否则可以移除手柄。 I've attached a snippet on how to do this below. 我已经附上了一个如何在下面执行此操作的片段。 The key thing is to override shouldPaintExpandControl in BasicTreeUI . 关键是在BasicTreeUI覆盖shouldPaintExpandControl

jtree.setUI(new BasicTreeUI() {
    @Override
    protected boolean shouldPaintExpandControl(final TreePath path, final int row
            , final boolean isExpanded, final boolean hasBeenExpanded, final boolean isLeaf)
    {
        boolean shouldDisplayExpandControl = false;
        return shouldDisplayExpandControl;
    }

This should really be documented in the JTree API but that's another issue. 这应该在JTree API中记录,但这是另一个问题。


Another approach to consider: 另一种考虑方法:

If you call DefaultTreeModel(TreeNode root, boolean asksAllowsChildren) the model will "ask" the nodes you insert if they are allowed to have children. 如果调用DefaultTreeModel(TreeNode root, boolean asksAllowsChildren) ,模型将“询问”您插入的节点(如果允许它们生成子节点)。 If they cannot, it should not display the expand icon. 如果他们不能,则不应显示展开图标。

Be sure to override javax.swing.tree.TreeNode.getAllowsChildren() in your class. 请务必在您的类中覆盖javax.swing.tree.TreeNode.getAllowsChildren()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM