2012-02-29 78 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()); 
    } 
} 

希望這可以幫助其他人,因爲我覺得線程的揭幕戰已經想通了。

相關問題