//Titel:        JTree
//Version:       1.1
//Copyright:     Copyright (c)
//Autor:         Dipl.-Inf., Dipl.-Ing. (FH) Michael Wilhelm
//Organisation:  HS Harz
//Beschreibung:  Einführung in JTree

// Beispiel mit JTree, DefaultMutableTreeNode

import java.awt.*;
import javax.swing.*;

import java.awt.event.*;

import javax.swing.tree.DefaultMutableTreeNode;

import java.io.*;

public class Tree02 extends JFrame {

  JTree jTree1;

  public Tree02() {
    setGUI();
  }

  private void setGUI() {
    DefaultMutableTreeNode root;


    setSize(400, 300);
    setTitle("JTree 2. Beispiel");
    this.getContentPane().setLayout( new BorderLayout() );

      // BuildTree generiert den Baum, gibt die Wurzel zurück
    root = BuildTree();
    jTree1 = new javax.swing.JTree(root);

    this.getContentPane().add(jTree1, BorderLayout.CENTER);
    // Neue Version
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  } // setGUI


  // BuildTree
  DefaultMutableTreeNode BuildTree() {
    DefaultMutableTreeNode root;
      // pro Stufe ein Knoten
    DefaultMutableTreeNode[]  Knoten = new DefaultMutableTreeNode[3];

    for (int i=0; i<3; i++) {
      Knoten[i] = new DefaultMutableTreeNode();
      //Knoten[i] = null;
    }


    root = new DefaultMutableTreeNode("root");

    Knoten[0] = new DefaultMutableTreeNode("1. Stufe (A)");
    root.add( Knoten[0] );

    Knoten[1] = new DefaultMutableTreeNode("AAA");
    Knoten[0].add( Knoten[1] );

    Knoten[1] = new DefaultMutableTreeNode("BBB");
    Knoten[0].add( Knoten[1] );

    // ---------

    Knoten[0] = new DefaultMutableTreeNode("1. Stufe (B)");
    root.add( Knoten[0] );

    Knoten[1] = new DefaultMutableTreeNode("CCC");
    Knoten[0].add( Knoten[1] );

    Knoten[1] = new DefaultMutableTreeNode("DDD");
    Knoten[0].add( Knoten[1] );

    return root;
  }  // BuildTree
  

  //Überschreiben, damit das Programm bei Herunterfahren des Systems beendet werden kann
  /*
  protected void processWindowEvent(WindowEvent e) {
    super.processWindowEvent(e);
    if(e.getID() == WindowEvent.WINDOW_CLOSING) {
      System.exit(0);
    }
  }
  */

  public static void main(String[] args) {
    Tree02 frame = new Tree02();
    frame.setVisible(true);
 }


}


