Solved

JTree(Vector v)  - problems displaying hierarchy

Posted on 2003-12-01
9
913 Views
Last Modified: 2007-12-19
Hi,

In my Applet I want to populate a JTree with a Vector using constructor JTree(Vector v). The Vector contains objects of a class (MyTreeNode) extending DefaultMutableTreeNode interface, some objects are children of others. I was hoping the hierarchi was displayed in the tree, but the tree is "flat", without sub-levels. I tried to add only the top parent objects to the Vector, but their children nodes were not displayed.

How can I get the hierarchi displayed in a JTree using the JTree(Vector v) constructor?



MyTreeNode root = new MyTreeNode("new Result("Result 1"));
MyTreeNode child_1 = new MyTreeNodenew Result("Result 1.1");
MyTreeNode child_2 = new MyTreeNode(new Result("Result 1.2");
MyTreeNode child_11 = new MyTreeNode(new Result("Result 1.1.1");
MyTreeNode child_12 = new MyTreeNode(new Result("Result 1.1.2");
root.add(child_1);
root.add(child_2);
child_1.add(child_11);
child_1.add(child_12);

Vector v = new Vector();
v.add(root);
v.add(child_1);
v.add(child_2);
v.add(child_11);
v.add(child_12);
// ...or...
//v.add(root);//displays only: Result 1

JTree tree = new JTree(v);




I also tried

JTree tree = new JTree(root);

but only root is displayed. I have tried to use a DefaultTreeModel:

DefaultTreeModel model = new DefaultTreeModel(root);
JTree tree = new JTree(model);

but root is the only node displayed...

Thanks for helping!!




0
Comment
Question by:MarteJ
  • 4
  • 2
  • 2
  • +1
9 Comments
 
LVL 7

Expert Comment

by:grim_toaster
ID: 9850905
The reason all of your nodes are on the same level is that the constructor that takes the Vector has this to say in the API docs:

Returns a JTree with each element of the specified Vector as the child of a new root node which is not displayed
0
 
LVL 2

Assisted Solution

by:SuperKarateMonkey
SuperKarateMonkey earned 150 total points
ID: 9850929
Your approach uses the wrong constructor.  I'm afriad the constructor:

JTree( Vector value )

is SUPPOSED to just create a JTree with all the elements in the Vector as the children in a flat tree underneath an undisplayed root.

See the notes under:

http://java.sun.com/j2se/1.4.2/docs/api/javax/swing/JTree.html#JTree(java.util.Vector)
0
 
LVL 7

Expert Comment

by:grim_toaster
ID: 9850959
Here's a very simple JTree example that does work, it shouldn't be too hard to modify to your needs:

import javax.swing.JFrame;
import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;

public class Thing extends JFrame
{
    public Thing() {
        super("Sample Tree");
        setSize(400, 400);
       
        DefaultMutableTreeNode root = new DefaultMutableTreeNode("top");
        DefaultMutableTreeNode child1 = new DefaultMutableTreeNode("child1");
        DefaultMutableTreeNode child2 = new DefaultMutableTreeNode("child2");
        DefaultMutableTreeNode grandChild = new DefaultMutableTreeNode("grandChild");
       
        root.add(child1);
        root.add(child2);
        child1.add(grandChild);
       
        JTree tree = new JTree(root);
       
        this.getContentPane().add(tree);
        this.show();
    }
   
    public static void main(String[] args) {
        new Thing();
    }
}
0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 2

Expert Comment

by:SuperKarateMonkey
ID: 9850977
Yup.  That'll do it.
0
 
LVL 92

Expert Comment

by:objects
ID: 9852437
> DefaultTreeModel model = new DefaultTreeModel(root);
> JTree tree = new JTree(model);

That should have worked.

What does MyTreeNode look like?
0
 
LVL 92

Accepted Solution

by:
objects earned 350 total points
ID: 9853429
Have you overidden isLeaf() in your MyTreeNode class?
0
 

Author Comment

by:MarteJ
ID: 9856417
Sorry for not posting MyTreeNode, that would have made it easier for you all.

Yes, I have overriden isLeaf and other methods of DefaultMutableTreeNode.... Shouldn't do that, should I?

I removed the overidings, and it worked - both

JTree tree = new JTree(root);

and

JTree tree = new JTree(model);

The overridings were left from when I implemented TreeNode interface, but when extending DMTreeNode, its methods should not be overidden. Got it!

Thanks for all help, everybody!!! :-)
0
 
LVL 92

Expert Comment

by:objects
ID: 9856424
> Shouldn't do that, should I?

You can, as long as you overide it with the correct behaviour :)
0
 
LVL 92

Expert Comment

by:objects
ID: 9856496
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question