Solved

Problem of Creating JTree with Hashtable ?

Posted on 2000-04-18
2
318 Views
Last Modified: 2008-03-06
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
Comment
Question by:andric
2 Comments
 
LVL 15

Expert Comment

by:lyonst
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

by:
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…

929 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

12 Experts available now in Live!

Get 1:1 Help Now