2012-02-29 37 views
8

我知道(我查看了源代码;)),在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 
. 
. 
.

在我看来,它ressembles简单的表格排序(所以调用禁用分拣分级限制解除)。

我看到2点的可能性,这样做:

  1. 中重新启用JXTable用的排序机制,从已实现的一切受益(分拣机,通过点击标题排序,...)和只对第一级的节点进行排序(因为他们的子节点仍然具有相同的父节点)。
  2. 实现我需要的基本东西(通过点击标题进行排序),在JXSortableTreeModel中有一个model-row-id列表,我排序并覆盖convertRowIndexToModel,这将允许(我认为)显示我的项目排序为I想。

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

说我解决这个问题,我将如何限制排序到第一级节点?即使在查看源文件后,我对如何做到这一点也无能为力。

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

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

非常感谢提前!

回答

3

maybe sorting TreeTable following way(s),通过aephyr,顺便说一句这个秋千大师打了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

+0

谢谢!我浏览过TreeTable的源代码,我必须说,这看起来很有希望。我会尽力在以后看看:) – ixM 2012-03-01 14:41:30

+1

Aephyr的解决方案效果很好,但源代码几乎没有任何文档或评论。演示是一个很好的例子,但我仍然遇到了EDT上抛出的几个异常。调试和解决代码中的这些问题和将来的问题是头疼的问题。 – Nate 2012-07-26 14:59:00

+0

@nate这是由伟大的Swing大师代码展览,代码仓库为aephyr的成员的几个论坛,不知道关于EDT例外或侵犯,必须从您的代码的次要问题:-) – mKorbel 2012-07-26 15:06:06

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()); 
    } 
} 

希望这可以帮助其他人,因为我觉得线程的揭幕战已经想通了。

相关问题