Solved

# Problem of Creating JTree with Hashtable ?

Posted on 2000-04-18
335 Views
hi,

Currently I'm facing a problem with the node order of the JTree generated from the Hashtable. The order in which objects are added to a Hashtable exhibit an unpredictable node order. It seems that there has no correlation to the manner in which the objects are stored in the Hashtable.

eg. the order to put a object into the Hashtable.

Hashtable ht = new Hashtable();
ht.put("Page 1", page1Vector);
ht.put("Page 2", page2Vector);

JTree jt = new JTree(ht);

The node order of the JTree might be "Page 2" as a first node and "Page 1" as a second node which is not in the desired order.

Is there any way to solve the ordering problem or sorting the Hashtable ?

Hope to hear from you soon, thank you.
0
Question by:andric
[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

LVL 15

Expert Comment

ID: 2726407
Hi,

I'm not sure if this helps but it is an example of how to load a Hashtable form SUN - there dosn't appear to be a specific sort order but could you use the integer value to loop and read the values in the correct sequence?.

This example creates a hashtable of numbers. It uses the names of the numbers as keys:

Hashtable numbers = new Hashtable();
numbers.put("one", new Integer(1));
numbers.put("two", new Integer(2));
numbers.put("three", new Integer(3));

To retrieve a number, use the following code:

Integer n = (Integer)numbers.get("two");
if (n != null) {
System.out.println("two = " + n);
}

T.
0

LVL 3

Accepted Solution

ovidiucraciun earned 100 total points
ID: 2726544
>>The node order of the JTree might
>>be "Page 2" as a first node and "Page
>>1" as a second node which is not in
>>the desired order

That means you should insert like this
Hashtable ht = new Hashtable();
ht.put("Page 2", page2Vector);
ht.put("Page 1", page1Vector);

0

## Featured Post

Question has a verified solution.

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

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 about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
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 …
###### Suggested Courses
Course of the Month4 days, 4 hours left to enroll