1

我有这样的层次结构:

Lot
 |_Stages
    |_Samples

例子:

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

这个想法是用户将提供一个 LotID 并基于此我的 SQL 查询检索所有阶段以及每个阶段内的所有样本。

我将结果集中的每条记录保存为自定义对象并将其添加到数组列表中。

结果集输出示例:

批号 || 批次条件 || 阶段ID || 阶段条件 || 样品编号 || 样品条件

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

5001 || 处置|| 4001 || 完成 || 3001 || 得到正式认可的

5001 || 处置|| 4001 || 完成 || 3002 || 得到正式认可的

5001 || 处置|| 4002 || 完成 || 3003 || 得到正式认可的

5001 || 处置|| 4002 || 完成 || 3004 || 得到正式认可的

5002 || 准备发布|| 4003 || 完成 || 3005 || 得到正式认可的

5003 || 处置|| 4004 || 完成 || 3006 || 得到正式认可的

5004 || 处置|| 4004 || 完成 || 3007 || 得到正式认可的

从这个数组列表中,我使用 DefaultMutableTreeNode 将对象添加到树中(即 new DefaultMutableTreeNode(custom_object.getLotID)..etc)。

现在有2个问题:

  1. 只有一个唯一的 LotID = 5001,但它显示了 4 次(来自上面的示例),因为有 4 个样本,因此有 4 条记录。

  2. 我的目标是首先检索 SampleID 并将其添加到各自的 StageID,最后将所有 StageID 添加到 LotID(根节点)。我不知道如何以分层方式显示。

谁能指出我正确的方向或一些想法?

4

1 回答 1

0

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 i.e. 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.

于 2013-03-01T19:58:23.700 回答