Search code examples
javaswingjtree

How to create a JTree structure from array data?


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:

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.

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


Solution

  • 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:

    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:

    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();
    }
    }