如何从数组数据创建JTree结构?

时间:2021-07-27 12:36:16

I have this table:

我有这张桌子:

+---------------------+
| Name | Type | Month |
+---------------------+
| John | xyz  | March |
+---------------------+
| Joe  | xyz  | March |
+---------------------+
| Nick | abc  | March |
+---------------------+
| Phil | abc  | March |
+---------------------+

I'd like to show its records on a JTree in this way:

我想用这种方式在JTree上显示它的记录:

March
| xyz
| | John
| | Joe
| abc
  | Nick
  | Phil

How can I do that?

我怎样才能做到这一点?

I found a lot of documentation about this but I still cannot understand how to do this from an array with an unknown length.

我发现了很多关于这方面的文档,但我仍然无法理解如何从长度未知的数组中做到这一点。

I'm asking this because with UCanAccess I select all records and I store them all in an array.

我问这个是因为使用UCanAccess我选择所有记录并将它们全部存储在一个数组中。

So what could be the most efficient way to add these nodes to the structure? (considering that I've 300+ elements to store)

那么将这些节点添加到结构中的最有效方法是什么? (考虑到我要存储300多个元素)

1 个解决方案

#1


1  

Loop the array and add a tree node if type or month changes. This is a very simplified example. It uses a class Test2.java describing the array. Make sure, your data comes in the correct sorted way:

如果类型或月份更改,则循环数组并添加树节点。这是一个非常简单的例子。它使用描述数组的类Test2.java。确保您的数据以正确的排序方式进行:

public class Test2 {
private String month;
private String type;
private String name;


public Test2(String month, String type, String name) {
    setMonth(month);
    setType(type);
    setName(name);
}

/**
 * @return the month
 */
public String getMonth() {
    return month;
}

/**
 * @param month the month to set
 */
public void setMonth(String month) {
    this.month = month;
}

/**
 * @return the Type
 */
public String getType() {
    return type;
}

/**
 * @param Type the Type to set
 */
public void setType(String type) {
    this.type = type;
}

/**
 * @return the name
 */
public String getName() {
    return name;
}

/**
 * @param name the name to set
 */
public void setName(String name) {
    this.name = name;
}

}

And this class displays a JTree in a JFrame:

此类在JFrame中显示JTree:

import java.awt.BorderLayout;
import java.util.ArrayList;
import java.util.Iterator;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;


public class Test1 extends JFrame {

public Test1() {
    super("Test");

    ArrayList<Test2> list = new ArrayList();
    list.add(new Test2("March", "xyz", "John"));
    list.add(new Test2("March", "xyz", "Joe"));
    list.add(new Test2("March", "abc", "Nick"));
    list.add(new Test2("March", "abc", "Phil"));

    Iterator iter = list.iterator();
    String prevMonth = "";
    String prevType = "";

    DefaultMutableTreeNode top = new DefaultMutableTreeNode("List");
    DefaultMutableTreeNode month = null;
    DefaultMutableTreeNode type  = null;

    while (iter.hasNext()) {
        Test2 t = (Test2) iter.next();
        if (!t.getMonth().equals(prevMonth)) {                
            if (month != null) {                   
                top.add(month);
            }
            month = new DefaultMutableTreeNode(t.getMonth());
            prevMonth = t.getMonth();

        }
        if (!t.getType().equals(prevType)) {                
            if (type != null) {                    
                month.add(type);
            }
            type = new DefaultMutableTreeNode(t.getType());
            prevType = t.getType();
        }            
        type.add(new DefaultMutableTreeNode(t.getName()));
    }
    month.add(type);
    top.add(month);
    this.getContentPane().setLayout(new BorderLayout());
    this.getContentPane().add(new JScrollPane(new JTree(top)));

    this.pack();
    this.setVisible(true);
    this.setDefaultCloseOperation(EXIT_ON_CLOSE);

}

public static void main(String a[]) {
    Test1 t1 = new Test1();
}
}

#1


1  

Loop the array and add a tree node if type or month changes. This is a very simplified example. It uses a class Test2.java describing the array. Make sure, your data comes in the correct sorted way:

如果类型或月份更改,则循环数组并添加树节点。这是一个非常简单的例子。它使用描述数组的类Test2.java。确保您的数据以正确的排序方式进行:

public class Test2 {
private String month;
private String type;
private String name;


public Test2(String month, String type, String name) {
    setMonth(month);
    setType(type);
    setName(name);
}

/**
 * @return the month
 */
public String getMonth() {
    return month;
}

/**
 * @param month the month to set
 */
public void setMonth(String month) {
    this.month = month;
}

/**
 * @return the Type
 */
public String getType() {
    return type;
}

/**
 * @param Type the Type to set
 */
public void setType(String type) {
    this.type = type;
}

/**
 * @return the name
 */
public String getName() {
    return name;
}

/**
 * @param name the name to set
 */
public void setName(String name) {
    this.name = name;
}

}

And this class displays a JTree in a JFrame:

此类在JFrame中显示JTree:

import java.awt.BorderLayout;
import java.util.ArrayList;
import java.util.Iterator;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;


public class Test1 extends JFrame {

public Test1() {
    super("Test");

    ArrayList<Test2> list = new ArrayList();
    list.add(new Test2("March", "xyz", "John"));
    list.add(new Test2("March", "xyz", "Joe"));
    list.add(new Test2("March", "abc", "Nick"));
    list.add(new Test2("March", "abc", "Phil"));

    Iterator iter = list.iterator();
    String prevMonth = "";
    String prevType = "";

    DefaultMutableTreeNode top = new DefaultMutableTreeNode("List");
    DefaultMutableTreeNode month = null;
    DefaultMutableTreeNode type  = null;

    while (iter.hasNext()) {
        Test2 t = (Test2) iter.next();
        if (!t.getMonth().equals(prevMonth)) {                
            if (month != null) {                   
                top.add(month);
            }
            month = new DefaultMutableTreeNode(t.getMonth());
            prevMonth = t.getMonth();

        }
        if (!t.getType().equals(prevType)) {                
            if (type != null) {                    
                month.add(type);
            }
            type = new DefaultMutableTreeNode(t.getType());
            prevType = t.getType();
        }            
        type.add(new DefaultMutableTreeNode(t.getName()));
    }
    month.add(type);
    top.add(month);
    this.getContentPane().setLayout(new BorderLayout());
    this.getContentPane().add(new JScrollPane(new JTree(top)));

    this.pack();
    this.setVisible(true);
    this.setDefaultCloseOperation(EXIT_ON_CLOSE);

}

public static void main(String a[]) {
    Test1 t1 = new Test1();
}
}