Solved

JTree aggregation

Posted on 2003-12-02
10
304 Views
Last Modified: 2008-02-01
I have a dynamic JTree that has numerous root nodes that appear as duplicates (via name), yet the children may be different.  I'm interested in allowing the user to aggregate the tree so the root nodes only appear once, yet all the tree nodes appear under them -- without having to make additional calls to the backend to get the data again.

So for instance, if the tree looks like this
- User
    + Josh
    + Frank
    + Susan
- Admin
    + Tom
    + Dick
    + Harry
- User
    + Johnny
    + Rick
    + Dave
- Admin
   + Tommy
   
I'd like it to appear as follows:
- User
   - User
       + Josh
       + Frank
       + Susan
   - User
       + Johnny
       + Rick
       + Dave
- Admin
    - Admin
       + Tom
       + Dick
       + Harry
    - Admin
       + Tommy
0
Comment
Question by:sapientconceptions
  • 5
  • 4
10 Comments
 
LVL 92

Expert Comment

by:objects
ID: 9861008
Either add the option in your table model to do the aggregate, or write a new table model that wraps your current model and performs the aggregation.
The second approach should be cleaner.
0
 

Author Comment

by:sapientconceptions
ID: 9861221
can you provide an example?
0
 
LVL 92

Accepted Solution

by:
objects earned 125 total points
ID: 9861299
Not specifically, the skeleton would be something like:

public AggregateTableModel extends AbstracTableModel
   implements TableModelListener
{
   private TableModel Wrapped;

  public AggregateTableModel(TableModel model)
  {
     Wrapped = model;
     // listen for changes to wrapped model
     Wrapped.addTasbleModelListener(this);
  }

  // you then need to implement TableModel methods
  // to return aggregated table details

  ...

   // and TableModelListener methods
   // to fire appropriate events when wrapped model changes
   
   ...
}
0
 

Author Comment

by:sapientconceptions
ID: 9870668
Thanks for you help. But this didn't help me at all.  I'm hoping someone can provide me the methods for re-arranging the children from one parent to another.

Thanks.
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 92

Expert Comment

by:objects
ID: 9871112
do u actually need both views?
0
 

Author Comment

by:sapientconceptions
ID: 9876320
yes...
0
 
LVL 92

Expert Comment

by:objects
ID: 9877037
is the data dynamic or static?
0
 

Author Comment

by:sapientconceptions
ID: 9877567
Data is dynamic.  But only two levels are initially populated with the original tree ("User/Admin" and their children).  So I'd like to iterate through the tree check a list to see if the node's value exists (i.e. User) if not, add node to new tree and map (record position in new tree), then add current node & children from original tree as child of newly added node to new tree.

Repeat, however if node exists in list, then find position of node in new tree and add node and kids to that parent node in tree.

hope that makes sense.
0
 
LVL 92

Expert Comment

by:objects
ID: 9877817
you could do that ie maintain two seperate tree models externally, or use the approach I suggested above to have the alternate view wrap your existing model and handle the mapping.
Either way it's fairly involved, but am happy to help when you have problems.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
strCopies  challenge 17 73
What is JNDI datasource in spring 1 37
firstChar challenge 13 83
maven project error 5 20
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

707 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