Search...
FAQs
Subscribe
Pie
FAQs
Recent topics
Flagged topics
Hot topics
Best topics
Search...
Search within Swing / AWT / SWT
Search Coderanch
Advance search
Google search
Register / Login
Post Reply
Bookmark Topic
Watch Topic
New Topic
programming forums
Java
Mobile
Certification
Databases
Caching
Books
Engineering
Micro Controllers
OS
Languages
Paradigms
IDEs
Build Tools
Frameworks
Application Servers
Open Source
This Site
Careers
Other
Pie Elite
all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Tim Cooke
Campbell Ritchie
paul wheaton
Ron McLeod
Devaka Cooray
Sheriffs:
Jeanne Boyarsky
Liutauras Vilda
Paul Clapham
Saloon Keepers:
Tim Holloway
Carey Brown
Piet Souris
Bartenders:
Forum:
Swing / AWT / SWT
Creating JTree On Run TIme
Kinu Kanwar
Ranch Hand
Posts: 33
posted 19 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
I have a data in this form
int node [] = 0 , 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8
int parent[] = -1 , 0 , 0 , 1 , 1 , 3 , 3 , 2 , 2
For example
node[A] = X
parent[A] = parent of X
and I ahve to draw Tree depending on relation b/w two arrays .... Any help is appreciated ...
Regards
Kinu Kanwar
Stuart Gray
Ranch Hand
Posts: 410
posted 19 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
I think something like this should work:
add node[0] as the tree root node for (int i = 1; i < node.length; i++) { add node[i] as child of the node indicated by parent[i] }
Here is some
Java
code (I haven't tested it though):
JTree tree = new JTree(nodes[0], true); for (int i = 1; i < nodes.length; i++) { nodes[parent[i]].add(nodes[i]); }
This assumes that nodes is an array of TreeNodes, which you could achieve like this:
TreeNode[] nodes = new DefaultMutableTreeNode[node.length]; for (int i = 0; i < node.length; i++) { nodes[i] = new DefaultMutableTreeNode(i); }
Kinu Kanwar
Ranch Hand
Posts: 33
posted 19 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Its not working :-(
Kinu
Stuart Gray
Ranch Hand
Posts: 410
posted 19 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Doesn't compile or doesn't run? Whats the error or problem you are getting?
Craig Wood
Ranch Hand
Posts: 1535
posted 19 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
import java.awt.*; import java.util.Enumeration; import javax.swing.*; import javax.swing.tree.*; public class NodeIndexing { public NodeIndexing() { int[] nodeIndices = { 0, 1, 2, 3, 4, 5, 6, 7, 8 }; int[] parentIndices = { -1, 0, 0, 1, 1, 3, 3, 2, 2 }; JTree tree = getTree(nodeIndices, parentIndices); JFrame f = new JFrame(); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.getContentPane().add(new JScrollPane(tree)); f.setSize(400,400); f.setLocation(200,200); f.setVisible(true); } private JTree getTree(int[] nodes, int[] parents) { DefaultMutableTreeNode root = new DefaultMutableTreeNode("root"); for(int j = 0; j < nodes.length; j++) { String s = String.valueOf(nodes[j]); DefaultMutableTreeNode node = new DefaultMutableTreeNode(s); if(parents[j] == -1) root.add(node); else { Enumeration allNodes = root.breadthFirstEnumeration(); while(allNodes.hasMoreElements()) { DefaultMutableTreeNode next = (DefaultMutableTreeNode)allNodes.nextElement(); String id = (String)next.getUserObject(); if(id.equals("root")) continue; int n = Integer.parseInt(id); if(n == parents[j]) { next.add(node); break; } } } } DefaultTreeModel model = new DefaultTreeModel(root); return new JTree(model); } public static void main(String[] args) { new NodeIndexing(); } }
Consider Paul's
rocket mass heater
.
reply
reply
Bookmark Topic
Watch Topic
New Topic
Boost this thread!
Similar Threads
How do i generate dynamic graph in html
July Newsletter Puzzle (Maze Solver)
How to maintain Parent-Child Data
Head First Java - Can't understand the output
Operators
More...