gpt4 book ai didi

java - 按字母顺序对 Jtree 节点排序

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:03:26 24 4
gpt4 key购买 nike

我已加载我的 JTree 以查看我的目录结构,如我的代码和输出图像所示。在这里,树节点默认按字母顺序排序,但我的另一个要求是我想根据目录名称的第二个名称对所有节点进行排序,而不实际重命名目录。我已在需要对 JTree 节点进行排序的名称下划线。请给我一些建议。

import java.io.File;
import javax.swing.JFrame;
import javax.swing.JTree;
import javax.swing.event.TreeModelListener;
import javax.swing.tree.TreeModel;
import javax.swing.tree.TreePath;

public class FILE_NAME {
public static void main(String[] args) {
JFrame frame = new JFrame("My Jtree");

File root = new File("C:/java");
JTree tree = new JTree(new FileTreeModel(root));
frame.setSize(300, 300);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(tree);
frame.setVisible(true);
}
}

class FileTreeModel implements TreeModel {

protected File root;

public FileTreeModel(File root) {
this.root = root;
}

@Override
public Object getRoot() {
return root;
}

@Override
public boolean isLeaf(Object node) {
return ((File) node).isFile();
}

@Override
public int getChildCount(Object parent) {
String[] children = ((File) parent).list();
if (children == null) {
return 0;
}
return children.length;
}

@Override
public Object getChild(Object parent, int index) {
String[] children = ((File) parent).list();
if ((children == null) || (index == children.length)) {
return null;
}
return new File((File) parent, children[index]);
}

@Override
public int getIndexOfChild(Object parent, Object child) {
String[] children = ((File) parent).list();
String childname = ((File) child).getName();
if (children == null) {
return -1;
}
for (int i = 0; i == children.length; i++) {
if (childname.equals(children[i])) {
return i;
}
}
return -1;
}

@Override
public void valueForPathChanged(TreePath path, Object newvalue) {
}

@Override
public void addTreeModelListener(TreeModelListener l) {
}

@Override
public void removeTreeModelListener(TreeModelListener l) {
}
}

输出

enter image description here

最佳答案

最灵活的解决方案是构建 DefaultMutableTreeNode 的简单扩展,每次添加新元素时都会对节点的子节点进行排序(一般想法归功于 this article):

public class SimpleTreeNode
extends DefaultMutableTreeNode
{
private final Comparator comparator;

public SimpleTreeNode(Object userObject, Comparator comparator)
{
super(userObject);
this.comparator = comparator;
}

public SimpleTreeNode(Object userObject)
{
this(userObject,null);
}

@Override
public void add(MutableTreeNode newChild)
{
super.add(newChild);
if (this.comparator != null)
{
Collections.sort(this.children,this.comparator);
}
}
}

此解决方案非常灵活,因为它允许您对树的每个级别甚至每个文件夹使用不同的排序方法。 (当然,您也可以非常轻松地在任何地方使用相同或不使用 Comparator。)

如果这对任何人有帮助,请参阅下面我用于 SimpleTreeNode 的两种排序方法:

public class Comparators
{
/** Allows alphabetical or reverse-alphabetical sorting
*
*/

public static class AlphabeticalComparator
implements Comparator
{
private final boolean order;

public AlphabeticalComparator()
{
this(true);
}

public AlphabeticalComparator(boolean order)
{
this.order = order;
}

@Override
public int compare(Object o1, Object o2)
{
if (order)
{
return o1.toString().compareTo(o2.toString());
}
else
{
return o2.toString().compareTo(o1.toString());
}
}
}

/** Allows sorting according to a pre-defined array
*
*/

public static class OrderComparator
implements Comparator
{
private final String[] strings;

public OrderComparator(String[] strings)
{
this.strings = strings;
}

@Override
public int compare(Object o1, Object o2)
{
String s1 = o1.toString();
String s2 = o2.toString();
int i1 = -1;
int i2 = -1;
for (int j = 0; j < strings.length; j++)
{
if (s1.equals(strings[j]))
{
i1 = j;
}
if (s2.equals(strings[j]))
{
i2 = j;
}
}
if (i1 == -1 || i2 == -1)
{
throw new Error("Can't use this comparator to compare "+o1+" and "+o2);
}
else
{
return Integer.compare(i1,i2);
}
}
}
}

关于java - 按字母顺序对 Jtree 节点排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9714328/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com