java 扩展特定的 JTree 路径

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11256159/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 04:24:54  来源:igfitidea点击:

Expanding specific JTree path

javaswingjtree

提问by Ivo

I have a problem with expanding JTree nodes. I start when use selects a node everything but the path to the selected node and the selected node itself to be collapsed. I've tried using

我在扩展 JTree 节点时遇到问题。当使用选择一个节点时,我开始选择除所选节点的路径和要折叠的所选节点本身之外的所有内容。我试过使用

tree.collapsePath(new TreePath(tree.getModel().getRoot()));
tree.expandPath(new TreePath(e111.getPath()));

something like this but it seems to have no effect and I really have no idea how can I do this

像这样的东西,但它似乎没有效果,我真的不知道我该怎么做

Here's how my tree looks:

这是我的树的外观:

http://img220.imageshack.us/img220/3450/jtreepng.png

http://img220.imageshack.us/img220/3450/jtreepng.png

If I click 1.1.1 I want everything else to be collapsed but the 1.1.1 node and the elements from the path to it.

如果我单击 1.1.1,我希望折叠其他所有内容,但 1.1.1 节点和路径中的元素除外。

Here's the application I've created:

这是我创建的应用程序:

public class SelectableTree extends JFrame implements TreeSelectionListener {
public static void main(String[] args) {
    new SelectableTree();
}

DefaultMutableTreeNode root;
DefaultMutableTreeNode e1;
DefaultMutableTreeNode e2;
DefaultMutableTreeNode e3;

DefaultMutableTreeNode e11;
DefaultMutableTreeNode e22;
DefaultMutableTreeNode e33;

DefaultMutableTreeNode e111;
DefaultMutableTreeNode e222;
DefaultMutableTreeNode e333;

DefaultMutableTreeNode aChild;
private JTree tree;
private JTextField currentSelectionField;

public SelectableTree() {
    super("JTree Selections");
    WindowUtilities.setNativeLookAndFeel();
    addWindowListener(new ExitListener());
    Container content = getContentPane();
    root = new DefaultMutableTreeNode("Root");

    e1 = new DefaultMutableTreeNode("1");
    e2 = new DefaultMutableTreeNode("2");
    e3 = new DefaultMutableTreeNode("3");

    e11 = new DefaultMutableTreeNode("1.1");
    e22 = new DefaultMutableTreeNode("2.2");
    e33 = new DefaultMutableTreeNode("3.3");

    e111 = new DefaultMutableTreeNode("1.1.1");
    e222 = new DefaultMutableTreeNode("2.2.2");
    e333 = new DefaultMutableTreeNode("3.3.3");

    root.add(e1);
    root.add(e2);
    root.add(e3);
    e1.add(e11);
    e2.add(e22);
    e3.add(e33);
    e11.add(e111);
    e22.add(e222);
    e33.add(e333);

    tree = new JTree(root);
    tree.addTreeSelectionListener(this);
    content.add(new JScrollPane(tree), BorderLayout.CENTER);
    currentSelectionField = new JTextField("Current Selection: NONE");
    content.add(currentSelectionField, BorderLayout.SOUTH);
    setSize(250, 275);
    setVisible(true);
}

public void valueChanged(TreeSelectionEvent event) {
    tree.clearSelection();
    tree.collapsePath(new TreePath(tree.getModel().getRoot()));
    tree.expandPath(new TreePath(e111.getPath()));
}

Edit: strangely enough the tree.collapsePathworks just fine, it seems that tree.expandPath(new TreePath(e111.getPath()));is the problem

编辑:奇怪的是,tree.collapsePath工作正常,似乎 tree.expandPath(new TreePath(e111.getPath()));是问题所在

采纳答案by trashgod

Swing GUI objects should be constructed and manipulated onlyon the event dispatch thread.

Swing GUI 对象应该事件分派线程上构造和操作。

enter image description here

在此处输入图片说明

import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.JTree;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreePath;

public class SelectableTree extends JFrame implements TreeSelectionListener {

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

            @Override
            public void run() {
                new SelectableTree();
            }
        });
    }
    DefaultMutableTreeNode root;
    DefaultMutableTreeNode e1 = new DefaultMutableTreeNode("1");
    DefaultMutableTreeNode e2 = new DefaultMutableTreeNode("2");
    DefaultMutableTreeNode e3 = new DefaultMutableTreeNode("3");
    DefaultMutableTreeNode e11 = new DefaultMutableTreeNode("1.1");
    DefaultMutableTreeNode e22 = new DefaultMutableTreeNode("2.2");
    DefaultMutableTreeNode e33 = new DefaultMutableTreeNode("3.3");
    DefaultMutableTreeNode e111 = new DefaultMutableTreeNode("1.1.1");
    DefaultMutableTreeNode e222 = new DefaultMutableTreeNode("2.2.2");
    DefaultMutableTreeNode e333 = new DefaultMutableTreeNode("3.3.3");
    DefaultMutableTreeNode aChild;
    private JTree tree;
    private JTextField currentSelectionField;

    public SelectableTree() {
        super("JTree Selections");
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        root = new DefaultMutableTreeNode("Root");
        root.add(e1);
        root.add(e2);
        root.add(e3);
        e1.add(e11);
        e2.add(e22);
        e3.add(e33);
        e11.add(e111);
        e22.add(e222);
        e33.add(e333);

        tree = new JTree(root);
        tree.addTreeSelectionListener(this);
        add(new JScrollPane(tree), BorderLayout.CENTER);
        currentSelectionField = new JTextField("Current Selection: NONE");
        add(currentSelectionField, BorderLayout.SOUTH);
        setSize(250, 275);
        setLocationRelativeTo(null);
        setVisible(true);
    }

    @Override
    public void valueChanged(TreeSelectionEvent event) {
        tree.expandPath(new TreePath(e11.getPath()));
        currentSelectionField.setText(event.getPath().toString());
    }
}

回答by Henry Lazarte

Use

利用

tree.setSelectionPath(new TreePath(e111.getPath()));

instead of

代替

tree.expandPath(new TreePath(e111.getPath()));

回答by Robin

Have you checked the javadoc of the expandPathmethod. The method does exactly what it describes:

您是否检查过该expandPath方法的javadoc 。该方法完全符合它的描述:

If the last item in the path is a leaf, this will have no effect.

如果路径中的最后一项是叶子,这将不起作用。