Solved

How do I populate a JTree from a database?

Posted on 2006-12-01
2
1,719 Views
Last Modified: 2008-01-09
Hello experts,

I have a table that consists of employees.

====================================
|  id              |     name                   |      manager      |
====================================
|    1             |   Albert                    |           0             |
|    2             |   Betty                     |           15            |
|    3             |   Charles                  |           12           |
|    4             |   Denise                   |           3             |
|    5             |   Elizabeth                |           8             |
|    6             |   Frank                     |           1             |
|    7             |   George                  |           12            |
|    8             |   Harriet                   |           2             |
|    9             |   Isabelle                 |           11            |
|    10           |   Jerry                     |           3              |
|    11           |   Katie                     |           12            |
|    12           |   Linda                     |           6             |
|    13           |   Mike                      |           11           |
|    14           |   Norman                 |           5             |
|    15           |   Oscar                    |           1             |
|    16           |   Pattrick                  |           3             |
|    17           |   Quincy                   |           11           |
====================================
As you can probably guess the manager column of a record contains a number which is the id of the record of the manager.
I am trying to build a JTree for a swing application. The JTree's structure should be based on the hierarchy of the data.
I have tried pulling all the data into a Person object and populating a Vector with Persons. When I use the Vector to buld DefaultTreeNodes I get a mess.
I would like someone to describe to me in detail a good way to build a TreeModel for my JTree?

Thank you for your help.  
0
Comment
Question by:binaryjohn
2 Comments
 
LVL 6

Expert Comment

by:valipotor
ID: 18055938
0
 

Accepted Solution

by:
dkrukovsky earned 500 total points
ID: 18056915
Hi John,

This is my first answer here.

Here is one of possible ways on how to accomplish this, in pseudocode.

You can create

class Manager {
List getManagedPersons() // returns list of Persons managed
}

class ManagerRepository {
List getManagers() // returns list of Managers
}

class ManagerTreeModel extends AutomatedTreeModel {
  ManagerRepository managerRepository;
  public ManagerTreeModel(ManagerRepository aManagerRepository) {
    managerRepository = aManagerRepository;
    Iterator it = managerRepository .getManagers().iterator();
    while(it.hasNext) {
      Manager manager = it.next();
      ManagerNode node = new ManagerNode(manager);
      addToNodeList(node); // adds node to the tree
    }
}

class ManagerNode extends AutomatedTreeNode {
  public ManagerNode (Manager aManager) {
    Iterator it = aManager.getManagedPersons().iterator();
    while(it.hasNext) {
      Person person = it.next();
      PersonNode node = new PersonNode(person);
      addToChildren(node); // adds node to this node children list
    }
}


AutomatedTreeModel / TreeNode classes are from my dotuseful library: http://dotuseful.sourceforge.net/

Let me know if you find this useful.

Denis Krukovsky
http://blogoforum.com/blog/dkrukovsky
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

Title # Comments Views Activity
Non-recursive backtracking, using a stack 1 98
Java / Linux and Regular Expressions 11 69
stackato and cloud 4 65
java jdbc batch example 8 20
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)
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 learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

914 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

18 Experts available now in Live!

Get 1:1 Help Now