0

How can i add dynamically(from textField )nodes to jtree in another jframe? here is my little code :

final First_JFRame frame= new First_JFRame();
DefaultTreeModel model = (DefaultTreeModel) frame.tree.getModel();
DefaultMutableTreeNode root = (DefaultMutableTreeNode) model.getRoot();
          DefaultMutableTreeNode firstNode = new DefaultMutableTreeNode("firstNode") ;
          root.add(firstNode);
    firstNode.add(new DefaultMutableTreeNode("firstleaf"));
    firstNode.add(new DefaultMutableTreeNode("secondleaf"));
    model.reload(root);

knowing that jtree declared as public static

h_salman
  • 11
  • 2
  • 1
    What part of that question are you actually have problems with? Adding nodes to a `JTree`? Passing information from one class to another? Knowing when the user has something to add? – MadProgrammer Mar 11 '16 at 21:22
  • i actually tried to add the value in my text field in frame2 as a node to jtree in frame1 but it did not work,but in the same jframe it worked – h_salman Mar 11 '16 at 21:27
  • *"..from another jframe"* 1) See [The Use of Multiple JFrames, Good/Bad Practice?](http://stackoverflow.com/q/9554636/418556) 2) For better help sooner, post a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). – Andrew Thompson Mar 11 '16 at 21:53
  • 1
    *"knowing that `jtree` declared as public static"* It probably should be neither (public nor static). – Andrew Thompson Mar 11 '16 at 21:54

1 Answers1

1

Here is a quick example on how to get desired action. Basically you just need to pass the instance of you Jtree to second frame and than add Nodes to it.

public static void main(String[] args) {

        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                // TODO Auto-generated method stub
                JFrame frame1 = new JFrame();
                frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame1.setLocationRelativeTo(null);


                JTree tree = new JTree();
                DefaultTreeModel model = (DefaultTreeModel) tree.getModel();
                DefaultMutableTreeNode root = (DefaultMutableTreeNode) model.getRoot();
                DefaultMutableTreeNode firstNode = new DefaultMutableTreeNode("firstNode") ;
                root.add(firstNode);
                firstNode.add(new DefaultMutableTreeNode("firstleaf"));
                firstNode.add(new DefaultMutableTreeNode("secondleaf"));
                model.reload();
                frame1.add(tree);
                frame1.pack();
                frame1.setVisible(true);

                JFrame frame2 = new JFrame();
                frame2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame2.setLocationRelativeTo(frame1);
                JTextField text = new JTextField(15);
                JButton add = new JButton("ADD");
                frame2.setLayout(new FlowLayout());
                frame2.add(text);
                frame2.add(add);
                frame2.pack();
                frame2.setVisible(true);

                add.addActionListener(new ActionListener() {

                    @Override
                    public void actionPerformed(ActionEvent e) {
                        firstNode.add(new DefaultMutableTreeNode(text.getText()));
                        model.reload();
                    }
                });
            }
        });

    }
Pendula
  • 688
  • 6
  • 17