[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

JTree(Vector v)  - problems displaying hierarchy

Posted on 2003-12-01
9
Medium Priority
?
939 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 600 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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 1400 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Suggested Courses

650 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