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

// JFrame mit ComboBox
// mit Editormöglichkeit

/*
Object getSelectedItem()
Object[] getSelectedObjects()
*/


public class ComboBox1 extends JFrame {

  JComboBox combo;
  JCheckBox cbBox1;

  //Frame konstruieren
  public ComboBox1() {
    setSize(500, 350);
    setTitle("JComboBox 1");
    setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
    setGUI();
  }


 private void setGUI() {
    JButton bn1;
    JLabel label1;
    String[] items;
    JButton BnOk;
    JButton BnEsc;


    this.getContentPane().setLayout ( new GridBagLayout() );

    label1 = new JLabel("Fachbereiche");
    label1.setFont( new Font("Arial", Font.BOLD,32));

    items = new String[20];
    for (int i=0; i<20; i++) {
      items[i] = Integer.toHexString(i+1)+" Auswahl";
    }

    combo = new JComboBox(items);
    combo.setFont( new Font("Arial", Font.BOLD,32));
    cbBox1 = new JCheckBox("Editierbar", false);

    this.getContentPane().add(label1, 
             new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0
            ,GridBagConstraints.WEST, GridBagConstraints.NONE, 
             new Insets(10,10,10,10), 0, 0));

    this.getContentPane().add(combo, 
             new GridBagConstraints(0, 1, 1, 1, 1.0, 0.0
            ,GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, 
             new Insets(10,10,10,10), 0, 0));
    this.getContentPane().add(cbBox1, 
             new GridBagConstraints(0, 2, 1, 1, 1.0, 0.0
            ,GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, 
             new Insets(10,10,10,10), 0, 0));

      // in der ComboBox darf nicht editiert werden
    combo.setEditable(false);
    combo.setSelectedIndex(-1);

    cbBox1.setFont( new Font("Arial", Font.BOLD,32));
    cbBox1.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(ActionEvent e) {
        cbBox1_actionPerformed(e);
      }
    });


    //   Schalter unten
    JPanel panel = new JPanel();
    panel.setLayout( new FlowLayout() );
    BnOk = new JButton("Ok");  // Label erzeugen
    panel.add(BnOk);
    BnOk.setFont( new Font("Arial", Font.BOLD,32));


    BnOk.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(ActionEvent e) {
        BnOk_click(e);
      }
    });

    BnEsc = new JButton("Esc");  // Label erzeugen
    panel.add(BnEsc);
    BnEsc.setFont( new Font("Arial", Font.BOLD,32));

    BnEsc.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(ActionEvent e) {
        BnEsc_click(e);
      }
    });

    this.getContentPane().add(panel, 
             new GridBagConstraints(0, 3, 1, 1, 1.0, 0.0
            ,GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, 
             new Insets(0,0,10,0), 0, 0));

  } // setGUI


  void cbBox1_actionPerformed(ActionEvent e) {
    combo.setEditable( cbBox1.isSelected()  );
  }


  void BnOk_click(ActionEvent e) {
     String sRes;
     int i;

     i = combo.getSelectedIndex();
     sRes = (String) combo.getSelectedItem();

    System.out.println("Der "+Integer.toString(i)+" Eintrag wurde gewaehlt");
    System.out.println("Ergebnis: "+sRes);
     // drei Fälle
     // -1  res=null
     // -1  res!=null
     // >=0
  }  // Bn Ok Click


  void BnEsc_click(ActionEvent e) {
      System.exit(0);
  }  // Bn ESC Click


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


  

}
