某一个节点: object: parent / MutableTreeNode: parentNodeDefaultTreeModel dtm=(DefaultTreeModel) tree.getModel();
int childNum=dtm.getChildCount(parent);
Object[] objs=new Object[childNum];
for(int i=0;i<childNum;i++){
Object obj=dtm.getChild(parent,i);
objs[i]=obj;
}
Arrays.sort(objs); //or use your compartor: sort(objs, compartor)//remove original nodes
//insert nodes from objs
tree.setModel(dtm);
tree.repaint();
int childNum=dtm.getChildCount(parent);
Object[] objs=new Object[childNum];
for(int i=0;i<childNum;i++){
Object obj=dtm.getChild(parent,i);
objs[i]=obj;
}
Arrays.sort(objs); //or use your compartor: sort(objs, compartor)//remove original nodes
//insert nodes from objs
tree.setModel(dtm);
tree.repaint();
不过最好的方法是自己实现一个排序的AbstractTreeModel。