Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

How do I populate a JTree from a database?

Posted on 2006-12-01
2
Medium Priority
?
1,897 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
 

Accepted Solution

by:
dkrukovsky earned 1500 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

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

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

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This video teaches viewers about errors in exception handling.
Suggested Courses
Course of the Month20 days, 23 hours left to enroll

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