简体   繁体   中英

Dynamically create nodes in JTree from a SQL query

I have a hierarchical structure like this:

Lot
 |_Stages
    |_Samples

Example:

Lot1
 |_Stage1
     |_Sample11 
     |_Sample12 
 |_Stage2
     |_Sample21 
     |_Sample22 

The idea is that the user will provide a LotID and based on this my SQL query retrieves all the Stages and also all samples within each stage.

I save each record in the result set as a custom Object and add it to an Array List.

Example of Result Set output:

LotID || LotCondition || StageID || StageCondition || SampleID || SampleCondition

--------|| -------------- || ------------ || -------------------- || ------------|| ----------------

5001 || Dispositioned || 4001 || Complete || 3001 || Approved

5001 || Dispositioned || 4001 || Complete || 3002 || Approved

5001 || Dispositioned || 4002 || Complete || 3003 || Approved

5001 || Dispositioned || 4002 || Complete || 3004 || Approved

5002 || ReadyToRelease|| 4003 || Complete || 3005 || Approved

5003 || Dispositioned || 4004 || Complete || 3006 || Approved

5004 || Dispositioned || 4004 || Complete || 3007 || Approved

From this array list I use a DefaultMutableTreeNode to add the objects to the Tree (ie new DefaultMutableTreeNode(custom_object.getLotID)..etc).

Now there are 2 issues:

  1. There is only one unique LotID = 5001, but it displays 4 times (from example above) because there are 4 samples and hence 4 records.

  2. My objective is to first retrieve the SampleIDs and add it to their respective StageIDs and finally add all the StageIDs to the LotID (root node). I do not know how to display in a hierarchial manner.

Can anyone please point me in the right direction or some ideas?

Ok..I found a way to do this and thought I might share it. I did not use the MutableTreeModel.

I decided to create loops ie when the result set is retrived. For example: I fetch the result set for Stages and within in I try to fetch for all the Samples with in each Stage.

Now I had 2 options, one was to store these result sets into a DefaultMutableTreeNode or to store them in an ArrayList and HaspMap (for example: I could have created a HashMap for Stage IDs as my keys and its Samples in an ArrayList as its values. Then because we can pass in any type of object other than a DefaultMutableTreeNode object to a TreeModel, we can pass in this HashMap object and use a custom TreeModel to handle it. This would also require my objects saved in the Arraylists override the HashCode and Equals method.

But I think, for my application, it is simple to go with option 1.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM