如何根据来自另一个Jtree的选择更改事件动态填充JTree?

时间:2015-11-13 04:45:35

标签: java swing jtree

我试图根据来自另一个JTree(A)的“选择已更改”事件动态填充JTree(B)。我不确定这是否可能,但下面是我迄今为止所尝试过的,但它似乎不起作用。我不确定我哪里出错了,因为我对Java很新。

完整代码:

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.util.ArrayList;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.JTree;
import javax.swing.event.TreeSelectionListener;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.JLabel;

public class Dynamic_Tree extends JFrame {

    private JPanel contentPane;
    private JLabel label_Tree1_Selection;
    private JTree tree_2;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    Dynamic_Tree frame = new Dynamic_Tree();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    public void populate_tree2(String Tree1_Slection) {
        String[] Stream_A = {"Project_A_Stream1", "Project_A_Stream2", "Project_A_Stream3"};
        String[] Stream_B = {"Project_B_Stream1", "Project_B_Stream2", "Project_B_Stream3"};
        String[] Stream_C = {"Project_C_Stream1", "Project_C_Stream2", "Project_C_Stream3"};

        DefaultTreeModel model = (DefaultTreeModel) tree_2.getModel();
        DefaultMutableTreeNode root = (DefaultMutableTreeNode) model.getRoot();
        root.removeAllChildren();
        model.reload(root);

        if (Tree1_Slection == "Project_A_Stream") {
            for (String stringA : Stream_A) {
                root.add(new DefaultMutableTreeNode(stringA));
            }
        } else if (Tree1_Slection == "Project_B_Stream") {
            for (String stringB : Stream_B) {
                root.add(new DefaultMutableTreeNode(stringB));
            }
        } else if (Tree1_Slection == "Project_C_Stream") {
            for (String stringC : Stream_C) {
                root.add(new DefaultMutableTreeNode(stringC));
            }
        }

    }

    public ArrayList<String> ExtractProjectsList() {
        ArrayList<String> Project_List = new ArrayList<String>();
        Project_List.add("Project_A");
        Project_List.add("Project_B");
        Project_List.add("Project_C");
        return Project_List;
    }

    /**
     * Create the frame.
     */
    public Dynamic_Tree() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 825, 472);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(null);

        JTree tree_1 = new JTree();
        tree_1.addTreeSelectionListener(new TreeSelectionListener() {
            public void valueChanged(TreeSelectionEvent arg0) {
                String Project_Node = arg0.getNewLeadSelectionPath().getLastPathComponent().toString();
                populate_tree2(Project_Node);
                label_Tree1_Selection.setText(Project_Node);
            }
        });
        tree_1.setModel(new DefaultTreeModel(new DefaultMutableTreeNode("Projects") {
            {
                DefaultMutableTreeNode node_1;
                ArrayList<String> arrrrr = ExtractProjectsList();
                for (String string : arrrrr) {
                    node_1 = new DefaultMutableTreeNode(string);
                    add(node_1);
                }
            }
        }));
        tree_1.setBounds(24, 40, 191, 358);
        contentPane.add(tree_1);

        JTree tree_2 = new JTree();
        tree_2.setModel(new DefaultTreeModel(new DefaultMutableTreeNode("Streams")));
        tree_2.setBounds(268, 40, 191, 358);
        contentPane.add(tree_2);

        label_Tree1_Selection = new JLabel("");
        label_Tree1_Selection.setBounds(502, 41, 191, 34);
        contentPane.add(label_Tree1_Selection);
    }
}

0 个答案:

没有答案