0

I am trying to populate a JTree(B) dynamically based on a "selection changed" event from another JTree(A). I am not sure if this even possible at all, but below is what I have tried so far and it doesn't seem to work. I'm not sure where I'm going wrong as I'm fairly new to Java.

Complete Code:

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);
    }
}
Dennis Meng
  • 5,109
  • 14
  • 33
  • 36
Kevin
  • 183
  • 2
  • 11
  • In future, please consider providing a [**runnable example**](https://stackoverflow.com/help/mcve) which demonstrates your problem. This is not a code dump, but an example of what you are doing which highlights the problem you are having. This will result in less confusion and better responses – MadProgrammer Nov 13 '15 at 04:51
  • Thanks for your suggestion. will keep that in mind. I thought it would help if just not dump the code and rather explain the components in the code. – Kevin Nov 13 '15 at 05:01
  • Been able to run the code and seeing how it fits together is really very useful when dealing with something as complex as a Swing GUI, it's the small things that are often the problem – MadProgrammer Nov 13 '15 at 05:03
  • i have included the complete code. Please remove the duplicate tag on this which is irrelevant so that other can answer. – Kevin Nov 13 '15 at 05:17
  • `} else if (Tree1_Slection == "Project_C_Stream") {` is NOT how `String`'s are compared in Java, fix this, verify that it's not the cause of the problem and either ask a new question or update the code and I'll consider re-opening the question, otherwise it's just waste of time for other people – MadProgrammer Nov 13 '15 at 05:20
  • You also have a `NullPointerException` in your code, which would attract another close option – MadProgrammer Nov 13 '15 at 05:22
  • You're shadowing your variables, you declare `tree_2` as an instance field; `private JTree tree_2;`; but then redeclare it as a local variable in the constructor; `JTree tree_2 = new JTree();`; which cause a NPE in `populate_tree2`. Your also comparing `Project_A_Stream` (`B` and `C`) to `Tree1_Slection`, when the values are actually `Project_A`, `Project_B` or `Project_C`. Make sure you pay attention to the console output and use a logger or `System.out.println` and/or a debugger to inspect the values of your variables and execution logic – MadProgrammer Nov 13 '15 at 05:28
  • First of all apologies and shame on me for not paying attention to those little typos and local variable which actually solved the problem. Thanks a billion for your help. Apologies again and really appreciate your help. – Kevin Nov 13 '15 at 05:47

0 Answers0