Results 1 to 9 of 9
  1. #1
    Melki is offline Member
    Join Date
    Jun 2008
    Posts
    20
    Rep Power
    0

    Default Move JTree item to another JTree.

    I am going to have two JTree's, if I select one JTree's leaf node and click a move button then the the node should move to the other Jtree under the same parent node were the leaf node comes from. If there is no such parent node in the other JTree then it should create that node and want to move the leaf node under it. Kindly help me in this issue.

    Thanks in advance.

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    8

    Default

    This is easy enough using the methods found in the JTree, TreePath and DefaultMutableTreeNode classes. Develop a/some method/s to look through your target tree to find the target parent node and then remove the leaf from the first and add the desired node(s) to the second.
    Start with making the two trees and getting the selection event code working the way you want. Then write the traverse method needed to find the target nodes. By then you should have a good idea how to finish up.

  3. #3
    Melki is offline Member
    Join Date
    Jun 2008
    Posts
    20
    Rep Power
    0

    Default

    Hi,

    It's ok but for me I am having three parent nodes then how to identify that three. Kindly give your suggetion if possible give me a sample code.

    Thank you.

  4. #4
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    8

    Default

    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import java.util.Enumeration;
    import javax.swing.*;
    import javax.swing.tree.*;
    
    public class MovingLeaves implements ActionListener {
        JTree left;
        JTree right;
    
        public void actionPerformed(ActionEvent e) {
            if(left.getSelectionPath() != null) {
                moveNode(left, right);
            }
            if(right.getSelectionPath() != null) {
                moveNode(right, left);
            }
         }
    
         private void moveNode(JTree source, JTree target) {
            TreePath path = source.getSelectionPath();
            DefaultMutableTreeNode node =
                (DefaultMutableTreeNode)path.getLastPathComponent();
            if(!node.isLeaf() || findNode(node.toString(), target) != null) {
                return;
            }
            String parentId =
                ((DefaultMutableTreeNode)node.getParent()).toString();
            DefaultMutableTreeNode parent = findNode(parentId, target);
            DefaultTreeModel model = (DefaultTreeModel)target.getModel();
            DefaultMutableTreeNode child =
                new DefaultMutableTreeNode(node.toString());
            if(parent == null) {
                parent = new DefaultMutableTreeNode(parentId);
                parent.insert(child, 0);
                DefaultMutableTreeNode root =
                    (DefaultMutableTreeNode)model.getRoot();
                model.insertNodeInto(parent, root, root.getChildCount());
            } else {
                model.insertNodeInto(child, parent, parent.getChildCount());
            }
            ((DefaultTreeModel)source.getModel()).removeNodeFromParent(node);
        }
    
        private DefaultMutableTreeNode findNode(String s, JTree tree) {
            DefaultMutableTreeNode root =
                (DefaultMutableTreeNode)tree.getModel().getRoot();
            Enumeration e = root.breadthFirstEnumeration();
            while(e.hasMoreElements()) {
                DefaultMutableTreeNode node =
                    (DefaultMutableTreeNode)e.nextElement();
                if(node.toString().equals(s)) {
                    return node;
                }
            }
            return null;
        }
    
        private JScrollPane getCenterComponent() {
            left = new JTree(new DefaultTreeModel(getLeft()));
            expandTree(left);
            right = new JTree(new DefaultTreeModel(getRight()));
            expandTree(right);
            JPanel panel = new JPanel(new GridLayout(1,0));
            panel.add(new JScrollPane(left));
            panel.add(new JScrollPane(right));
            return new JScrollPane(panel);
        }
    
        private void expandTree(JTree tree) {
            DefaultMutableTreeNode root =
                (DefaultMutableTreeNode)tree.getModel().getRoot();
            Enumeration e = root.breadthFirstEnumeration();
            while(e.hasMoreElements()) {
                DefaultMutableTreeNode node =
                    (DefaultMutableTreeNode)e.nextElement();
                if(node.isLeaf()) continue;
                int row = tree.getRowForPath(new TreePath(node.getPath()));
                tree.expandRow(row);
            }
        }
    
        private DefaultMutableTreeNode getLeft() {
            String[] genera = {
                "Aquila", "Spizaetus", "Haliaeetus", "Hieraaetus", "Harpia"
            };
            String[] species = {
                "Golden Eagle", "Spanish Imperial Eagle", "Gurney's Eagle",
                "Mountain Hawk-eagle", "Philippine Hawk-eagle",
                "Bald Eagle", "African Fish-eagle",
                "New Guinea Hawk-eagle", "Harpy Eagle"
            };
            int[] indices = { 3, 2, 2, 1, 1 };
            int k = 0;
            DefaultMutableTreeNode root =
                new DefaultMutableTreeNode("Accipitridae");
            for(int i = 0; i < genera.length; i++) {
                DefaultMutableTreeNode node =
                    new DefaultMutableTreeNode(genera[i]);
                root.add(node);
                for(int j = 0; j < indices[i]; j++) {
                    node.add(new DefaultMutableTreeNode(species[k++]));
                }
            }
            return root;
        }
    
        private DefaultMutableTreeNode getRight() {
            String[] genera = {
                "Aquila", "Spizaetus", "Haliaeetus", "Hieraaetus",
                "Harpyhaliaetus"
            };
            String[] species = {
                "Steppe Eagle", "Indian Spotted Eagle",
                "Black Hawk-eagle", "Ornate Hawk-eagle",
                "White-tailed Eagle", "Madagascar Fish-eagle",
                "Ayres' Hawk-eagle", "African Hawk-eagle",
                "Solitary Eagle"
            };
            int[] indices = { 2, 2, 2, 2, 1 };
            int k = 0;
            DefaultMutableTreeNode root =
                new DefaultMutableTreeNode("Accipitridae");
            for(int i = 0; i < genera.length; i++) {
                DefaultMutableTreeNode node =
                    new DefaultMutableTreeNode(genera[i]);
                root.add(node);
                for(int j = 0; j < indices[i]; j++) {
                    node.add(new DefaultMutableTreeNode(species[k++]));
                }
            }
            return root;
        }
    
        private JPanel getLastComponent() {
            JButton button = new JButton("move");
            button.addActionListener(this);
            JPanel panel = new JPanel();
            panel.add(button);
            return panel;
        }
    
        public static void main(String[] args) {
            MovingLeaves test = new MovingLeaves();
            JFrame f = new JFrame();
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.add(test.getCenterComponent());
            f.add(test.getLastComponent(), "Last");
            f.setSize(500,400);
            f.setLocation(200,200);
            f.setVisible(true);
        }
    }

  5. #5
    Melki is offline Member
    Join Date
    Jun 2008
    Posts
    20
    Rep Power
    0

    Default

    Hi Senior,


    Thank you very much for your code it helped me a lot. It had given me some good ideas of how to make my code implemented.

    Thank you.

  6. #6
    Melki is offline Member
    Join Date
    Jun 2008
    Posts
    20
    Rep Power
    0

    Default

    Hi Senior,

    If there is no parent node in the right tree means, then we should create that appropriate parent node and then we should insert the child node inside it. What shall be the solution for this? Kindly explain.

    Thank you.

  7. #7
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    8

    Default

    Java Code:
    private void moveNode(JTree source, JTree target) {
        ...
        // [i]If there is no parent node in the right tree means,[/i]
        // Actually, if there is no parent node in the target tree
        if(parent == null) {
            // [i]then we should create that appropriate parent node[/i]
            parent = new DefaultMutableTreeNode(parentId);
            // [i]and then we should insert the child node inside it.[/i]
            parent.insert(child, 0);
            // Add this new parent node to the end of the root node
            // of the target tree model.
            DefaultMutableTreeNode root =
                (DefaultMutableTreeNode)model.getRoot();
            model.insertNodeInto(parent, root, root.getChildCount());

  8. #8
    Melki is offline Member
    Join Date
    Jun 2008
    Posts
    20
    Rep Power
    0

    Default

    I am really sorry for that I didn't noticed that. Here again I am getting another one new requirement that is there shall be multiple parent node say,

    |GrandParent
    ---|Parent
    ------|Child
    ---------|Leaf1
    ---------|Leaf2
    ---------|Leaf3

    In this case how could be the code altered.
    Last edited by Melki; 09-08-2008 at 08:24 AM.

  9. #9
    msankar.ravi is offline Member
    Join Date
    Jul 2009
    Posts
    9
    Rep Power
    0

    Default

    Like this code could we display the c: drive in Jtree view and add checkbox for files. is it possible.

Similar Threads

  1. JcheckBoxes as JTree Nodes
    By aneesahamedaa in forum AWT / Swing
    Replies: 11
    Last Post: 02-11-2009, 12:11 AM
  2. A Simple JTree Example
    By Java Tip in forum javax.swing
    Replies: 0
    Last Post: 06-27-2008, 07:41 PM
  3. Why my JTree always collasped after refreshing?
    By lmsook10 in forum AWT / Swing
    Replies: 2
    Last Post: 06-24-2008, 05:55 AM
  4. Node selection in jtree
    By Preethi in forum AWT / Swing
    Replies: 4
    Last Post: 06-19-2008, 11:25 PM
  5. JTree trouble
    By Alantie Vala in forum AWT / Swing
    Replies: 3
    Last Post: 07-31-2007, 11:12 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •