?
Solved

JTree(Vector v)  - problems displaying hierarchy

Posted on 2003-12-01
9
Medium Priority
?
933 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
Get real performance insights from real users

Key features:
- Total Pages Views and Load times
- Top Pages Viewed and Load Times
- Real Time Site Page Build Performance
- Users’ Browser and Platform Performance
- Geographic User Breakdown
- And more

 
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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.

Question has a verified solution.

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

Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses
Course of the Month13 days, 2 hours left to enroll

777 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