如何使JXTreeTable排序其顶级元素

时间:2012-02-29 12:53:40

标签: java swing jtable swingx 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,这将允许(我认为)显示我的项目排序为I想。
  3. 我最喜欢的当然是第一个,但我不知道如何实现它。第一个限制是我不知道如何重新启用排序,因为JXTreeTable重写了setSortable()(和所有相关的方法),我不知道如何直接访问JXTable中的方法实现(这是一个Java问题)。我可以简单地复制JXTreeTable的代码并删除覆盖,但在我看来这不是一个选项。

    说我解决了这个问题,我如何限制排序到第一级节点?即使在查看来源之后,我仍然对如何做到这一点毫无头绪。

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

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

    非常感谢提前!

2 个答案:

答案 0 :(得分:3)

maybe sorting TreeTable following way(s),作者:aephyr,顺便说一下,这个Swing Guru也玩过Nimbus Look and Feel,包括有趣的Nimbus代码

enter image description here enter image description here enter image description here enter image description here enter image description here enter image description here

答案 1 :(得分:1)

我设法通过在节点级别玩游戏来实现。这是我在树表节点中创建的一个方法,它扩展了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<Object, BRFTreeTableNode> 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<Map.Entry<Object, BRFTreeTableNode>> nodesIterator;
    if (sortAscending) {
        nodesIterator = nodeData.entrySet().iterator();
    } else {
        nodesIterator = nodeData.descendingMap().entrySet().iterator();
    }

    while (nodesIterator.hasNext()) {
        Map.Entry<Object, BRFTreeTableNode> nodeEntry = nodesIterator.next();
        this.add(nodeEntry.getValue());
    }
}

希望这有助于其他人,因为我认为开幕式已经解决了问题。