Java Usage of JTree #1

Part of the code is stolen from cited sources. In general, what is shown in blue is stuff generated by the IDE (not completely, however), and what is shown in black is my typing (I hope you know what I mean). I will annotate the text in color comments which should be self evident as to whether they are code or comments.
 

/*
 * TreeTest.java
 * minimal tree creation
 * Created on August 19, 2003, 2:33 PM
 */

/**
 *
 * @author  C. W. David
 * with some help from www.apl.jhu/~hall/java/Swing-Tutorial
 */
import javax.swing.*;
import javax.swing.tree.*;

public class TreeTest extends javax.swing.JFrame implements java.awt.event.WindowListener{
    
    /** Creates new form TreeTest */
    public TreeTest() {
        initComponents();


Here we creat the Nodes of the Tree

        DefaultMutableTreeNode root=new DefaultMutableTreeNode("Root");
        DefaultMutableTreeNode child, grandchild;
        for (int childIndex=1;childIndex<4;childIndex++){
            child=new DefaultMutableTreeNode("Child "+childIndex);
            root.add(child);//add each created child to root
            for (int grandChildIndex=1;grandChildIndex<4;grandChildIndex++){
                grandchild = new DefaultMutableTreeNode("GrandChild "+childIndex+"."+grandChildIndex);
                child.add(grandchild);//add each grandchild to each child
            }
        }

Here we create the treeModel

        DefaultTreeModel treeModel = new DefaultTreeModel(root);

Here we associate the treeModel with the actual tree which was created in the 
IDE

        jTree1.setModel(treeModel);

    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    private void initComponents() {//GEN-BEGIN:initComponents
        jTree1 = new javax.swing.JTree();

        addWindowListener(this);

        getContentPane().add(jTree1, java.awt.BorderLayout.CENTER);

        pack();
    }

    // Code for dispatching events from components to event handlers.

    public void windowActivated(java.awt.event.WindowEvent evt) {
    }

    public void windowClosed(java.awt.event.WindowEvent evt) {
    }

    public void windowClosing(java.awt.event.WindowEvent evt) {
        if (evt.getSource() == TreeTest.this) {
            TreeTest.this.exitForm(evt);
        }
    }

    public void windowDeactivated(java.awt.event.WindowEvent evt) {
    }

    public void windowDeiconified(java.awt.event.WindowEvent evt) {
    }

    public void windowIconified(java.awt.event.WindowEvent evt) {
    }

    public void windowOpened(java.awt.event.WindowEvent evt) {
    }//GEN-END:initComponents
    
    /** Exit the Application */
    private void exitForm(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_exitForm
        System.exit(0);
    }//GEN-LAST:event_exitForm
    
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        new TreeTest().show();
    }
    
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTree jTree1;
    // End of variables declaration//GEN-END:variables
    
}

Return to the main book TOC.