Solved

JTree(Vector v)  - problems displaying hierarchy

Posted on 2003-12-01
9
909 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
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 
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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Application launch issue with Apache Tomcat 5 42
Java Loop 6 59
Way to decrease size of apk file 9 65
Desingning Refactoring existing code 2 24
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…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
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…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

776 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