/******************************************************/
/* Prof. Dr. Carsten Vogt                             */
/* FH Koeln, Fak. 07 / Nachrichtentechnik             */
/* http://www.nt.fh-koeln.de/vogt                     */
/*                                                    */
/* Das Programm zeigt die Verwendung von Combo-Boxen. */
/******************************************************/

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

class ComboDemo extends JFrame {

  // Combo-Box mit den Strings, die sie enthalten soll:

  private JComboBox combo;
  String[] st = {"Item 1","Item 2","Item 3","Item 4","Item 5","Item 6","Item 7","Item 8","Quit"};

  // Textfeld, in dem angezeigt wird, welches Element der Combo-Box ausgewaehlt ist:

  private JTextField textausgabe;

  // Konstruktor: Erzeugung der Grafik-Oberflaeche

  ComboDemo(String s) {

   // Erzeugen und Positionieren des Fensterrahmens:

   super(s);
   setLocation(20,20);
 
   // Box für Combo-Box und Ausgabefeld:

   Box b = Box.createVerticalBox();
   getContentPane().add(b);

   // Ueberschrift:

   JLabel lab = new JLabel("Auswahl mit Combo-Box:");
   lab.setFont(new Font("Arial",Font.BOLD,22));
   lab.setForeground(Color.black);
   b.add(lab);
   b.add(Box.createVerticalStrut(10));

   // Erzeugung der Combo-Box (Konstruktorparameter: Strings, die in der Combo-Box stehen sollen):

   combo = new JComboBox(st);

   // Festlegung der Farben

   combo.setFont(new Font("Arial",Font.BOLD,22));
   combo.setForeground(Color.blue);

   // Vorauswahl: viertes Element

   combo.setSelectedIndex(3);

   // Listener für Reaktion bei Aenderung der Auswahl hinzufuegen:

   combo.addItemListener(new AuswahlListener());

   // Einfuegen der Combo-Box ins Fenster:

   Box b2 = Box.createHorizontalBox();
   b2.add(Box.createHorizontalGlue());
   b2.add(combo);
   b2.add(Box.createHorizontalGlue());
   b.add(b2);

   b.add(Box.createVerticalStrut(20));

   // Textkomponente zur Anzeige des ausgewaehlten Eintrags:

   lab = new JLabel("ausgewaehltes Item:");
   lab.setFont(new Font("Arial",Font.BOLD,22));
   lab.setForeground(Color.black);
   b.add(lab);

   textausgabe = new JTextField();
   textausgabe.setFont(new Font("Arial",Font.BOLD,22));
   textausgabe.setForeground(Color.black);
   b.add(textausgabe);

   // Komponenten anordnen und sichtbar machen:

   setLocation(100,100);
   pack();
   setVisible(true);

  }

  // Aktion bei Auswahl:

  class AuswahlListener implements ItemListener {

   public void itemStateChanged(ItemEvent e) {

    textausgabe.setText(combo.getSelectedItem().toString());     
    if (combo.getSelectedItem().toString().equals("Quit"))
     System.exit(1);

   }

  }

}

// Hauptprogramm

public class Combo {

  public static void main(String args[]) {

   // Hauptprogramm: Erzeugung der Oberflaeche

   new ComboDemo("Listen");  

  }

}

