如何使用JTree侦听器动态更改带有CardLayout的JPanel?

时间:2015-05-11 01:55:20

标签: java swing listener jtree cardlayout

我正在尝试使用JTree的内容使用CardLayout更改Panel。它只适用于最后的选择。我应该对我的代码做什么样的听众或更改?

我也在控制台中编写文本,似乎得到了正确的值。这就是为什么让我感到沮丧的原因。

单击第一级节点时,我的代码应显示第一级,单击第二级节点时,我的代码应显示第二级。我用了

DefaultMutableTreeNode selectedNode = (DefaultMutableTreeNode) tree
                                .getLastSelectedPathComponent();

它只影响最后的组件。我该如何解决?谢谢!

源代码:

import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Color;
import java.awt.Dimension;

import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTree;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;

public class ProblemTree2 extends JFrame {

    private DefaultMutableTreeNode root = new DefaultMutableTreeNode("Root");
    private DefaultTreeModel model = new DefaultTreeModel(root);
    private JTree tree = new JTree(model);
    private JPanel card;

    public ProblemTree2() {

        card = new JPanel(new CardLayout());
        card.setBorder(BorderFactory.createLineBorder(Color.BLACK));

        JLabel label1 = new JLabel("1st level");
        JLabel label2 = new JLabel("2nd level");

        DefaultMutableTreeNode n1 = new DefaultMutableTreeNode(
                "1st level: Child 1");
        n1.add(new DefaultMutableTreeNode("2nd level: Child l"));
        DefaultMutableTreeNode n2 = new DefaultMutableTreeNode(
                "1st level: Child 2");
        n2.add(new DefaultMutableTreeNode("2nd level: Child 2"));
        DefaultMutableTreeNode n3 = new DefaultMutableTreeNode(
                "1st level: Child 3");
        n3.add(new DefaultMutableTreeNode("2nd level: Child 3"));


        card.add(label1,"1st level: Child 1");
        card.add(label1,"1st level: Child 2");
        card.add(label1,"1st level: Child 3");

        card.add(label2,"2nd level: Child l");
        card.add(label2,"2nd level: Child 2");
        card.add(label2,"2nd level: Child 3");

        root.add(n1);
        root.add(n2);
        root.add(n3);

        tree.setEditable(true);
        tree.setSelectionRow(0);
        tree.setRootVisible(true);
        tree.setShowsRootHandles(true);


        tree.getSelectionModel().addTreeSelectionListener(
                new TreeSelectionListener() {
                    @Override
                    public void valueChanged(TreeSelectionEvent e) {

                        final CardLayout cards = (CardLayout) card
                                .getLayout();
                        DefaultMutableTreeNode selectedNode = (DefaultMutableTreeNode) tree
                                .getLastSelectedPathComponent();
                        System.out.println(selectedNode.toString());
                        cards.show(card,selectedNode.toString());
                    }
                });


        JScrollPane scrollPane = new JScrollPane(tree);
        scrollPane.setPreferredSize(new Dimension(500,500));
        getContentPane().add(scrollPane, BorderLayout.WEST);
        getContentPane().add(card, BorderLayout.CENTER);

        setSize(1000, 600);
        setVisible(true);
    }

    public static void main(String[] arg) {
        ProblemTree2 pt = new ProblemTree2();
    }
}

1 个答案:

答案 0 :(得分:4)

此...

card.add(label1, "1st level: Child 1");
card.add(label1, "1st level: Child 2");
card.add(label1, "1st level: Child 3");

card.add(label2, "2nd level: Child l");
card.add(label2, "2nd level: Child 2");
card.add(label2, "2nd level: Child 3");

导致您的问题,您无法为组件分配多个密钥的相同实例。

当您尝试再次添加该组件时,它首先会从其父容器中删除,这会导致CardLayout删除" name"同样,只意味着......

card.add(label1, "1st level: Child 3");
// and...
card.add(label2, "2nd level: Child 3");

实际上正在运行(已添加到用户界面/ CardLayout

您必须为每个名称提供组件的新实例。

或者,您可以向TreeNode提供更多详细信息,以便您确定该级别并显示该级别的正确组件,这意味着您CardLayout中只有两个组件,每个级别一个。

DefaultMutableTreeNode允许您提供"用户" Object到它。默认情况下,它使用此对象的toString方法作为节点的文本,但您可以使用TreeCellRenderer自定义此方法或为您的toString方法提供值#34;自定义级别"对象