Let’s say, I have this code of a jTree
JTree jTree = new JTree() ;
DefaultMutableTreeNode root = new DefaultMutableTreeNode("root") ;
jTree.setModel( new DefaultTreeModel(root));
DefaultMutableTreeNode firstNode = new DefaultMutableTreeNode("first node") ;
Now I want to add a leaf into the node named “first node” ( which is currently a leaf ) from a place where firstNode variable is not available. How can it be done?
Clearification:
Okey! I have a JFrame in a class (extends swing) and a jTree is a private member of this class. Now I pass this class object (as a parameter) into another class method where the jTree is supposed to be populated. But I couldn’t do anything with that jTree from another class method. (Of course accessing private method is not my problem 🙂
Try this:
UPDATE: As per your comments, you must have a reference to the node under which you want to add the new leaf. If you are responding to an tree selection event in the tree, then you can get the reference by this: