【问题标题】:How to create a JTree structure from array data?如何从数组数据创建 JTree 结构?
【发布时间】:2017-01-27 10:27:52
【问题描述】:

我有这张桌子:

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

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

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

我该怎么做?

我找到了很多关于此的文档,但我仍然无法理解如何从长度未知的数组中执行此操作。

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

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

【问题讨论】:

  • UCanAccess 提供了 JDBC 接口。它不在数组中存储记录!
  • @jamadei 已编辑!那么你知道答案吗?
  • “我仍然无法理解如何从长度未知的数组中执行此操作” - 你知道你可以iterate through the elements in an array,对吧?
  • @GordThompson 实际上我有,考虑到我正在使用 NetBeans UI,我不知道如何将它们添加到树中

标签: java swing jtree


【解决方案1】:

如果类型或月份发生变化,则循环数组并添加一个树节点。这是一个非常简化的例子。它使用描述数组的类 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;
}

}

并且这个类在一个 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();
}
}

【讨论】:

    猜你喜欢
    • 2019-12-03
    • 2022-01-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-25
    • 2020-05-22
    • 2021-10-13
    相关资源
    最近更新 更多