Hi,

I have 2 JTree components on my JForm. If a user clicks on a DefaultMutableTreeNode on JTree A i want app to add it to JTree B.

public void addModule(final JTree a, final JTree b){
        a.addTreeSelectionListener(new TreeSelectionListener(){
            @Override
            public void valueChanged(TreeSelectionEvent e) {
                DefaultMutableTreeNode node = new DefaultMutableTreeNode(a.getLastSelectedPathComponent());
                b.add(node);
            }
        
        });
    }

I have been trying to implement this via MouseListener or TreeSelectionListener but
just cant do it right problem occurs at this location ( b.add(node); );

Recommended Answers

All 9 Replies

"problem occurs" isn't much to go on. Post the specific error message.

The only thing I could say at this point is that you need to add the node to another node, not the JTree component itself.

Hm the thing is that that JTree B that i am adding the node to already has a Node root node attached to it but in another package/class

Exception in thread "AWT-EventQueue-0" java.lang.RuntimeException: Uncompilable source code - Erroneous sym type: javax.swing.JTree.add
	at pft.controller.Tower_Controller$2.valueChanged(Tower_Controller.java:59)
	at javax.swing.JTree.fireValueChanged(JTree.java:2917)
	at javax.swing.JTree$TreeSelectionRedirector.valueChanged(JTree.java:3376)
	at javax.swing.tree.DefaultTreeSelectionModel.fireValueChanged(DefaultTreeSelectionModel.java:634)
	at javax.swing.tree.DefaultTreeSelectionModel.notifyPathChange(DefaultTreeSelectionModel.java:1092)
	at javax.swing.tree.DefaultTreeSelectionModel.setSelectionPaths(DefaultTreeSelectionModel.java:293)
	at javax.swing.tree.DefaultTreeSelectionModel.setSelectionPath(DefaultTreeSelectionModel.java:187)
	at javax.swing.JTree.setSelectionPath(JTree.java:1631)
	at javax.swing.plaf.basic.BasicTreeUI.selectPathForEvent(BasicTreeUI.java:2371)
	at javax.swing.plaf.basic.BasicTreeUI$Handler.handleSelection(BasicTreeUI.java:3587)
	at javax.swing.plaf.basic.BasicTreeUI$Handler.mousePressed(BasicTreeUI.java:3526)
	at java.awt.Component.processMouseEvent(Component.java:6501)
	at javax.swing.JComponent.processMouseEvent(JComponent.java:3321)
	at java.awt.Component.processEvent(Component.java:6269)
	at java.awt.Container.processEvent(Container.java:2229)
	at java.awt.Component.dispatchEventImpl(Component.java:4860)
	at java.awt.Container.dispatchEventImpl(Container.java:2287)
	at java.awt.Component.dispatchEvent(Component.java:4686)
	at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832)
	at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4489)
	at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422)
	at java.awt.Container.dispatchEventImpl(Container.java:2273)
	at java.awt.Window.dispatchEventImpl(Window.java:2713)
	at java.awt.Component.dispatchEvent(Component.java:4686)
	at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:707)
	at java.awt.EventQueue.access$000(EventQueue.java:101)
	at java.awt.EventQueue$3.run(EventQueue.java:666)
	at java.awt.EventQueue$3.run(EventQueue.java:664)
	at java.security.AccessController.doPrivileged(Native Method)
	at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
	at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
	at java.awt.EventQueue$4.run(EventQueue.java:680)
	at java.awt.EventQueue$4.run(EventQueue.java:678)
	at java.security.AccessController.doPrivileged(Native Method)
	at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
	at java.awt.EventQueue.dispatchEvent(EventQueue.java:677)
	at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:211)
	at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:128)
	at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:117)
	at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:113)
	at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:105)
	at java.awt.EventDispatchThread.run(EventDispatchThread.java:90)

Both of these 2 threes are created in a separate class. I wish to add nodes from one to another in this function whats the best way i can deal whit this.

Perhaps change the parameters to your addModule() method to DefaultMutableTreeNodes or at least pass tree model you wish to add the node to?

It's really a TreeModel B that you want to add nodes to from TreeModel A. Specifically there is some node in B that you want to attach a new child node to from model A.

Its really simple but i dont know how to implement this.

Anyways if anyone gets an idea how to get around this here is what i am trying to do:

1) I have a separate GUI class and a separate Logic class
2) I have 2 JTree components A and B
3) Once a user clicks on a node on A i want that node to be appended to B
4) I am using function bellow to call both JTree components and i am listening for node selection on JTree A once i click on one i wish to add it to B.

5) B already has 1 root node defined inside another class

Oke so to dismiss the post above i found a solution by creating a neutral TreeNodes than appending those to new tree via model.

Now i want to know is it possible to have a DefaultMutableTreeNode Table like:
DefaultMutableTreeNode[] nodes = DefaultMutableTreeNode();

Oke so this is my code so far it works well but there is a small problem, the arraylist and the function removeModule do not work correctly.

function removeModule does actually remove the node from the tree on GUI when displayed but not from the actual ArrayList because when i add one module back to that ArrayList the one that was so-pose to be removed is still in the list and will reappear.

ArrayList<DefaultMutableTreeNode> nodes = new ArrayList<DefaultMutableTreeNode>();    
    
    public void addModule(final JTree module_browser, final JTree selected_modules){
        module_browser.addTreeSelectionListener(new TreeSelectionListener(){
            @Override
            public void valueChanged(TreeSelectionEvent e) {
                DefaultMutableTreeNode node = new DefaultMutableTreeNode(module_browser.getLastSelectedPathComponent());
                nodes.add(node);
                updateModules(selected_modules);
            }
        
        });
    }
    
    public void removeModule(final JTree selected_modules){
        selected_modules.addTreeSelectionListener(new TreeSelectionListener(){

            @Override
            public void valueChanged(TreeSelectionEvent e) {
                DefaultMutableTreeNode node = new DefaultMutableTreeNode(selected_modules.getLastSelectedPathComponent());
                parseNodes(node);
                updateModules(selected_modules);
            }
        });
    }
    
    public void parseNodes(final DefaultMutableTreeNode node){
        for(int i = 0; i < nodes.size(); i++){
            if(nodes.get(i) == node){
                nodes.remove(i);
            }
        }
    }
    
    public void updateModules(final JTree selected_modules){
        //selected_modules.setModel(new DefaultTreeModel(nodes));
        DefaultMutableTreeNode hiracy = new DefaultMutableTreeNode("Selected Modules");
        for(int i = 0; i < nodes.size(); i++){
            hiracy.add(nodes.get(i));
        }
        selected_modules.setModel(new DefaultTreeModel(hiracy));
    }

I'm not seeing any reason to even have the ArrayList at all. You can add and remove nodes directly on the tree model.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.