排序是经常要用到的方法,目前发现了两种方法:
1在treemodal前加一个过滤器ModalFilter的抽象类
用图比较容易明白
这是没有添加Fileter的情况:以Jlist为例(JTree,JTable差不多的)
这是添加了以后的情况
就是不管做什么。先过filter这关再说。。。
再用些代码来说话吧。!
package com.ketherware.models;
import java.util.*;
import javax.swing.*;
public abstract class AlphaSortingListModelFilter extends
AbstractListModel
{
// 已排序的索引数组
protected ArrayList sortedIndex;
public AlphaSortingListModelFilter(ListModel delegate)
{
this.delegate = delegate;//这个是实现过滤的必要的
resort();
}
// 该算法称为“插入排序”,适合于处理元素个数少于几百个的数据。
// 它是一种“无堆栈”排序。
protected synchronized void resort()
{
sortedIndex = new ArrayList();
nextElement:
for (int x=0; x < delegate.getSize(); x++)
{
for (int y=0; y < x; y++)
{
String current =
delegate.getElementAt(x).toString();
int compareIndex =
((Integer) sortedIndex.get(y)).intValue();
String compare =
sortedIndex.get(compareIndex).toString();
if (current.compareTo(compare) < 0)
{
sortedList.add(new Integer(x), y);
continue nextElement;
}
}
sortedList.add(new Integer(x));
}
}
public Object getElementAt(int index)
{
// 委托给过滤器目标,但使用已排序的索引
return delegate.getElementAt(sortedIndex[index]);
}
}
这个方法的具体参考
http://www-128.ibm.com/developerworks/cn/java/j-filters/
2继承一个DefaultTreeModel类,使用super.insertNodeInto( )这个方法,把节点放入他应该在的位置
具体实现如下
1在treemodal前加一个过滤器ModalFilter的抽象类
用图比较容易明白
这是没有添加Fileter的情况:以Jlist为例(JTree,JTable差不多的)
这是添加了以后的情况
就是不管做什么。先过filter这关再说。。。
再用些代码来说话吧。!
package com.ketherware.models;
import java.util.*;
import javax.swing.*;
public abstract class AlphaSortingListModelFilter extends
AbstractListModel
{
// 已排序的索引数组
protected ArrayList sortedIndex;
public AlphaSortingListModelFilter(ListModel delegate)
{
this.delegate = delegate;//这个是实现过滤的必要的
resort();
}
// 该算法称为“插入排序”,适合于处理元素个数少于几百个的数据。
// 它是一种“无堆栈”排序。
protected synchronized void resort()
{
sortedIndex = new ArrayList();
nextElement:
for (int x=0; x < delegate.getSize(); x++)
{
for (int y=0; y < x; y++)
{
String current =
delegate.getElementAt(x).toString();
int compareIndex =
((Integer) sortedIndex.get(y)).intValue();
String compare =
sortedIndex.get(compareIndex).toString();
if (current.compareTo(compare) < 0)
{
sortedList.add(new Integer(x), y);
continue nextElement;
}
}
sortedList.add(new Integer(x));
}
}
public Object getElementAt(int index)
{
// 委托给过滤器目标,但使用已排序的索引
return delegate.getElementAt(sortedIndex[index]);
}
}
这个方法的具体参考
http://www-128.ibm.com/developerworks/cn/java/j-filters/
2继承一个DefaultTreeModel类,使用super.insertNodeInto( )这个方法,把节点放入他应该在的位置
具体实现如下
// SortTreeModel.java // This class is similar to the DefaultTreeModel, but it keeps // a node's children in alphabetical order. import javax.swing.tree.*; import java.util.Comparator; public class SortTreeModel extends DefaultTreeModel { private Comparator comparator; public SortTreeModel(TreeNode node, Comparator c) { super(node); comparator = c; } public SortTreeModel(TreeNode node, boolean asksAllowsChildren, Comparator c) { super(node, asksAllowsChildren); comparator = c; } public void insertNodeInto(MutableTreeNode child, MutableTreeNode parent) { int index = findIndexFor(child, parent);
super.insertNodeInto(child, parent, index); } public void insertNodeInto(MutableTreeNode child, MutableTreeNode par, int i) { // The index is useless in this model, so just ignore it. insertNodeInto(child, par); } // Perform a recursive binary search on the children to find the right // insertion point for the next node. private int findIndexFor(MutableTreeNode child, MutableTreeNode parent) { int cc = parent.getChildCount( ); if (cc == 0) { return 0; } if (cc == 1) { return comparator.compare(child, parent.getChildAt(0)) <= 0 ? 0 : 1; } return findIndexFor(child, parent, 0, cc - 1); // First and last index } private int findIndexFor(MutableTreeNode child, MutableTreeNode parent, int i1, int i2) { if (i1 == i2) { return comparator.compare(child, parent.getChildAt(i1)) <= 0 ? i1 : i1 + 1; } int half = (i1 + i2) / 2; if (comparator.compare(child, parent.getChildAt(half)) <= 0) { return findIndexFor(child, parent, i1, half); } return findIndexFor(child, parent, half + 1, i2); } }
参考Oreilly java swing 2nd 的chap 17.2
loop's blog