Solved

JTree(Vector v)  - problems displaying hierarchy

Posted on 2003-12-01
9
896 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
 
LVL 2

Expert Comment

by:SuperKarateMonkey
Comment Utility
Yup.  That'll do it.
0
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
LVL 92

Expert Comment

by:objects
Comment Utility
> 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
Comment Utility
Have you overidden isLeaf() in your MyTreeNode class?
0
 

Author Comment

by:MarteJ
Comment Utility
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
Comment Utility
> 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
Comment Utility
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Books that can get me started on JAVA 2 52
wordappend challenge 8 84
maven project error 5 19
github account with ecipse 1 15
For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
The viewer will learn how to implement Singleton Design Pattern in Java.

743 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now