Solved

How do I populate a JTree from a database?

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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
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…

760 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

20 Experts available now in Live!

Get 1:1 Help Now