Solved

How do I populate a JTree from a database?

Posted on 2006-12-01
2
1,741 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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

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 45
simple java question 3 56
Android development question 2 37
Glassfish admin console not working 1 14
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…
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…
This video teaches viewers about errors in exception handling.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

810 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