扩展特定的JTree路径

时间:2012-06-29 04:42:32

标签: java swing jtree

我在扩展JTree节点时遇到问题。我开始使用时选择一个节点,但是所选节点的路径和所选节点本身都要折叠。我尝试过使用

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

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

以下是我树的样子:

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

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

这是我创建的应用程序:

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()));
}

编辑:奇怪的是tree.collapsePath工作得很好,似乎tree.expandPath(new TreePath(e111.getPath()));就是问题

3 个答案:

答案 0 :(得分:7)

应在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());
    }
}

答案 1 :(得分:3)

您是否检查了expandPath方法的javadoc。该方法正如它所描述的那样:

  

如果路径中的最后一项是叶子,则无效。

答案 2 :(得分:3)

使用

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

而不是

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