阻止JTree中节点扩展的节点选择 [英] Prevent node selection on node expansion in a JTree

查看:109
本文介绍了阻止JTree中节点扩展的节点选择的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个 JTree ,其中一些节点是自定义的,以显示它们是可扩展的,尽管它们还没有任何子节点。我已经关注此主题实现它。

I have created a JTree with some of its nodes being custom to show them as expandable although they don't have any children (yet). I have followed this thread to implement that.

为什么?我想动态加载树,所以当树扩展时,我从服务器检索更多信息并在树上显示。

Why? I want do load the tree dynamically so when the tree gets expanded, I retrieve further information from the server and show it on the tree.

我遇到的问题是当我展开其中一个节点时,它会被选中,这不是默认节点的默认行为(您可以在不更改树选择的情况下展开它们)。

The problem I am experiencing is that when I expand one of these nodes, it becomes selected, which is not the default behavior for the default nodes (you can expand them without changing the tree selection).

如何解决这个问题是为了防止这个节点在扩展时被选中?

How to solve this to prevent this node to become selected on expansion?

推荐答案

我没有用下面的代码解决这个问题。如果没有看到你的代码,你必须做一些我们无法猜到的事情。尝试使用下面的代码并修改它以重现您看到的问题。通过这样做,您很有可能会发现您正在做的不同以及为什么会出现这种行为(另请参阅 SSCCE )。

I don't get that problem with the code below. You must be doing something else that we can't guess without seeing your code. Try taking the code below and modify it to reproduce the problem you are seeing. By doing that, there is a great chance that you will find what you are doing differently and why you get that behaviour (see also SSCCE).

代码取自这里

import java.awt.BorderLayout;
import java.awt.Container;

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTree;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreeNode;

public class DynamicTree extends JFrame {

    public class OutlineNode extends DefaultMutableTreeNode {
        private boolean areChildrenDefined = false;
        private int outlineNum;
        private int numChildren;

        public OutlineNode(int outlineNum, int numChildren) {
            this.outlineNum = outlineNum;
            this.numChildren = numChildren;
        }

        @Override
        public boolean isLeaf() {
            return false;
        }

        @Override
        public int getChildCount() {
            if (!areChildrenDefined) {
                defineChildNodes();
            }
            return super.getChildCount();
        }

        private void defineChildNodes() {
            // You must set the flag before defining children if you
            // use "add" for the new children. Otherwise you get an infinite
            // recursive loop, since add results in a call to getChildCount.
            // However, you could use "insert" in such a case.
            areChildrenDefined = true;
            for (int i = 0; i < numChildren; i++) {
                add(new OutlineNode(i + 1, numChildren));
            }
        }

        @Override
        public String toString() {
            TreeNode parent = getParent();
            if (parent == null) {
                return String.valueOf(outlineNum);
            } else {
                return parent.toString() + "." + outlineNum;
            }
        }
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                new DynamicTree(5);
            }
        });
    }

    public DynamicTree(int n) {
        super("Creating a Dynamic JTree");
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (InstantiationException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (UnsupportedLookAndFeelException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        Container content = getContentPane();
        JTree tree = new JTree(new OutlineNode(1, n));
        content.add(new JScrollPane(tree), BorderLayout.CENTER);
        setSize(300, 475);
        setVisible(true);
    }
}

这篇关于阻止JTree中节点扩展的节点选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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