Results 1 to 9 of 9
- 09-01-2008, 07:24 AM #1
Member
- Join Date
- Jun 2008
- Posts
- 20
- Rep Power
- 0
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.
- 09-01-2008, 09:58 PM #2
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.
- 09-02-2008, 07:10 AM #3
Member
- Join Date
- Jun 2008
- Posts
- 20
- Rep Power
- 0
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.
- 09-04-2008, 11:08 PM #4
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); } }
- 09-05-2008, 02:20 PM #5
Member
- Join Date
- Jun 2008
- Posts
- 20
- Rep Power
- 0
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.
- 09-08-2008, 07:20 AM #6
Member
- Join Date
- Jun 2008
- Posts
- 20
- Rep Power
- 0
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.
- 09-08-2008, 07:42 AM #7
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());
- 09-08-2008, 08:21 AM #8
Member
- Join Date
- Jun 2008
- Posts
- 20
- Rep Power
- 0
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.
- 07-09-2009, 11:59 AM #9
Member
- Join Date
- Jul 2009
- Posts
- 9
- Rep Power
- 0
Similar Threads
-
JcheckBoxes as JTree Nodes
By aneesahamedaa in forum AWT / SwingReplies: 11Last Post: 02-11-2009, 12:11 AM -
A Simple JTree Example
By Java Tip in forum javax.swingReplies: 0Last Post: 06-27-2008, 07:41 PM -
Why my JTree always collasped after refreshing?
By lmsook10 in forum AWT / SwingReplies: 2Last Post: 06-24-2008, 05:55 AM -
Node selection in jtree
By Preethi in forum AWT / SwingReplies: 4Last Post: 06-19-2008, 11:25 PM -
JTree trouble
By Alantie Vala in forum AWT / SwingReplies: 3Last Post: 07-31-2007, 11:12 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks