如何使JXTreeTable排序其顶级元素

我知道(我已经查看了源代码;))已经禁用了JXTreeTable上的排序。

但是,我想仅允许根据根节点的直接子节点的值对所有列进行排序。

说我有这样的结构:

Name / Date / File UID (Root) |- Mr. X / 1996/10/22 / AE123F6D |--- File1 / 2012/01/10 / DFC2345Q |--- File2 / 2012/01/11 / D321FEC6 |- Mrs. Y / 1975/03/03 / G2GF35EZ |--- File3 / 2012/02/29 / 35GERR32 |--- File4 / 2012/01/22 / LKJY2342 . . . 

我想要实现的是仅在第一级节点上对3列进行排序。 假设我想按升序日期对其进行排序,它最终会像这样:

 Name / Date / File UID (Root) |- Mrs. Y / 1975/03/03 / G2GF35EZ |--- File3 / 2012/02/29 / 35GERR32 |--- File4 / 2012/01/22 / LKJY2342 |- Mr. X / 1996/10/22 / AE123F6D |--- File1 / 2012/01/10 / DFC2345Q |--- File2 / 2012/01/11 / D321FEC6 . . . 

正如我所看到的,它重新组合到简单的表排序(因此解除了为禁用排序而调用的层次结构限制)。

我看到有两种可能性:

  1. 重新启用JXTable中可用的排序机制,以便从已经实现的所有内容中获益(排序器,通过单击标题进行排序,……)并且只对第一级的节点进行排序(因为他们的子节点仍然具有相同的父节点) 。
  2. 实现我需要的基本内容(通过主要点击标题排序),在JXSortableTreeModel中有一个模型行ID的列表,我排序并覆盖convertRowIndexToModel,这将允许(我认为)按我的意愿显示我的项目。

我最喜欢的当然是第一个,但我不知道如何实现它。 第一个限制是我不知道如何重新启用排序,因为JXTreeTable重写了setSortable()(和所有相关的方法),我不知道如何直接访问JXTable中的方法实现(这是一个Java问题)。 我可以简单地复制JXTreeTable的代码并删除覆盖,但在我看来这不是一个选项。

假设我解决了这个问题,我如何限制排序到第一级节点? 即使在查看源代码之后,我对如何做到这一点也很无能为力。

对于第二个,我放弃了现有代码的所有好处,但我看到了实现我想要的实用方法(至少现在,至少。谁知道是否不想过滤所有行上的数据?)。

还有其他方法吗? 如果没有,我应该选择哪一种解决方案? 任何有见地的评论?

提前谢谢了!

也许按照方式排序TreeTable ,通过aephyr,顺便说一句,这个Swing Guru也玩过Nimbus Look and Feel,包括有趣的Nimbus的代码

在此处输入图像描述在此处输入图像描述在此处输入图像描述在此处输入图像描述在此处输入图像描述在此处输入图像描述

我设法通过在节点级别玩游戏来实现。 这是我在树表节点中创建的方法,它扩展了AbstractMutableTreeTableNode:

 /** * This method recursively (or not) sorts the nodes, ascending, or descending by the specified column. * @param sortColumn Column to do the sorting by. * @param sortAscending Boolean value of weather the sorting to be done ascending or not (descending). * @param recursive Boolean value of weather or not the sorting should be recursively applied to children nodes. * @author Alex Burdu Burdusel */ public void sortNode(int sortColumn, boolean sortAscending, boolean recursive) { mLastSortAscending = sortAscending; mLastSortedColumn = sortColumn; mLastSortRecursive = recursive; int childCount = this.getChildCount(); TreeMap nodeData = new TreeMap(String.CASE_INSENSITIVE_ORDER); for (int i = 0; i < childCount; i++) { BRFTreeTableNode child = (BRFTreeTableNode) this.getChildAt(i); if (child.getChildCount() > 0 & recursive) { child.sortNode(sortColumn, sortAscending, recursive); } Object key = child.getData()[sortColumn]; nodeData.put(key, child); } Iterator> nodesIterator; if (sortAscending) { nodesIterator = nodeData.entrySet().iterator(); } else { nodesIterator = nodeData.descendingMap().entrySet().iterator(); } while (nodesIterator.hasNext()) { Map.Entry nodeEntry = nodesIterator.next(); this.add(nodeEntry.getValue()); } } 

希望这有助于其他人,因为我认为开启者已经弄明白了。