[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

How do I populate a JTree from a database?

Posted on 2006-12-01
2
Medium Priority
?
1,879 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
In this post we will learn different types of Android Layout and some basics of an Android App.
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Suggested Courses

656 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