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

import java.util.Vector;

// Beispiel mit Vector
// mit Klasse CPruefung

public class Liste05 extends JFrame {

   JList jlist;
   Vector liste;

  //Frame konstruieren
  public Liste05() {
     JButton bnOk;  // Aktionsschalter Links
     JButton bnESC;  // Aktionsschalter Abbruch
     JScrollPane scroll;

     setSize(420, 320);
     setTitle("liste Liste05");

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

      // Liste
    setData();  // create + insert
    jlist = new JList(liste);
    jlist.setSelectionMode( ListSelectionModel.MULTIPLE_INTERVAL_SELECTION );

    scroll = new JScrollPane(jlist);
    this.getContentPane().add(scroll, new GridBagConstraints(0, 0, 4, 1, 1.0, 1.0
            ,GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(20,20,0,20), 0, 0));
    /*
    SINGLE_SELECTION
    SINGLE_INTERVAL_SELECTION
    MULTIPLE_INTERVAL_SELECTION
    */

    bnOk = new JButton("Ok");  // Label erzeugen

    this.getContentPane().add(bnOk, new GridBagConstraints(0, 1, 1, 1, 1.0, 0.0
            ,GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(20,0,20,0), 0, 0));


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

    bnESC = new JButton("Abbruch");  // Label erzeugen
    this.getContentPane().add(bnESC, new GridBagConstraints(1, 1, 1, 1, 1.0, 0.0
            ,GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(20,0,20,0), 0, 0));

    bnESC.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(ActionEvent e) {
        bnESC_actionPerformed(e);
      }
    });
    JButton bn1=new JButton("1");  // Aktionsschalter Links
    JButton bn2=new JButton("2");  // Aktionsschalter Links
    JButton bn3=new JButton("3");  // Aktionsschalter Links
    JButton bn4=new JButton("4");  // Aktionsschalter Links
    this.getContentPane().add(bn1, new GridBagConstraints(0, 2, 1, 1, 1.0, 0.0
            ,GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(20,0,20,0), 0, 0));
    this.getContentPane().add(bn2, new GridBagConstraints(1, 2, 1, 1, 1.0, 0.0
            ,GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(20,0,20,0), 0, 0));
    this.getContentPane().add(bn3, new GridBagConstraints(2, 2, 1, 1, 1.0, 0.0
            ,GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(20,0,20,0), 0, 0));
    this.getContentPane().add(bn4, new GridBagConstraints(3, 2, 1, 1, 1.0, 0.0
            ,GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(20,0,20,0), 0, 0));

    bn1.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(ActionEvent e) {
        bn1_click();
      }
    });
    bn2.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(ActionEvent e) {
        bn2_click();
      }
    });


    bn3.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(ActionEvent e) {
        bn3_click();
      }
    });
    bn4.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(ActionEvent e) {
        bn4_click();
      }
    });

    jlist.setFont( new Font("Arial", Font.BOLD,18)  );
    bnOk.setFont( new Font("Arial", Font.BOLD,18)  );
    bnESC.setFont( new Font("Arial", Font.BOLD,18)  );
    bn1.setFont( new Font("Arial", Font.BOLD,18)  );
    bn2.setFont( new Font("Arial", Font.BOLD,18)  );

  } // create

  void bn1_click() {
    CPruefung.typ=0;
    jlist.updateUI();
  }
  void bn2_click() {
    CPruefung.typ=1;
    jlist.updateUI();
  }

  void bn3_click() {
    jlist.updateUI();
  }
  void bn4_click() {
    jlist.updateUI();
  }

   // Ausgabe der selektierten Elemente
  void bnOK_click(ActionEvent e) {
     CPruefung Result;
     Object[] elements = jlist.getSelectedValues();

     if (elements.length==0) {
       System.out.println("Kein Eintrag selektiert");
     }
     else {
       for (int i=0; i<elements.length; i++) {
          // Casting
         Result = (CPruefung) elements[i];  // Typecasting
         System.out.println( Result.toString() );
       }
       System.out.println("");
       System.out.println("");
     }
  }


  void bnESC_actionPerformed(ActionEvent e) {
      System.exit(0);
  }


     // Eintragen
    private void setData() {
      CPruefung prf;

      liste = new Vector();

      prf = new CPruefung(
        "Grundlagen der Informatik 1", 1.3);
      liste.add(prf);

      prf = new CPruefung( "Grundlagen der Informatik 2", 2.3);
      liste.add(prf);

      prf = new CPruefung( "Grundlagen der Informatik 3", 1.7);
      liste.add(prf);

      prf = new CPruefung( "XXMathematik 1", 2.3);
      liste.insertElementAt(prf,0);

      prf = new CPruefung( "Mathematik 2", 1.0);
      liste.add(prf);

      prf = new CPruefung( "Mathematik 3", 2.0);
      liste.add(prf);

      prf = new CPruefung( "Physik 1", 2.3);
      liste.add(prf);

      prf = new CPruefung( "Physik 2", 3.3);
      liste.add(prf);


      prf = new CPruefung( "Elektrotechnik 1", 3.0);
      liste.add(prf);

      prf = new CPruefung( "Elektrotechnik 2", 5.0);
      liste.add(prf);

    }  // setData


  //Ü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) {
    Liste05 frame = new Liste05();
    frame.setVisible(true);
  }


}




class CPruefung {
    public  static int typ=0;
    private String _Pruefung;
    private double _Note;

    public CPruefung() {
      this( "keinen Namen", 5.0);
    }

    public CPruefung( String Pruefung, double Note) {
      _Pruefung = Pruefung;
      _Note = Note;
    }


   public String getPruefung() {
     return _Pruefung;
   }
   public void setPruefung(String Pruefung) {
     _Pruefung = Pruefung;
   }


   public Double getNote() {
     return new Double (_Note);
   }
   public void setNote(double Note) {
     _Note = Note;
   }

   
 public String toString() {
    switch (typ) {
        case 0: return "Prüfung: "+_Pruefung;
        case 1: return "Note: "+_Note;
        case 2: return "Prüfung: "+_Pruefung+"    Note: "+_Note;
        case 3: return "Note: "+_Note+"      Prüfung: "+_Pruefung;
        default: return "xxxxx";
      }

   }


}  // CPruefungen









/*
  public String toString() {
  switch (typ) {
        case 0: return "Prüfung: "+_Pruefung;
        case 1: return "Note: "+_Note;
        case 2: return "Prüfung: "+_Pruefung+"    Note: "+_Note;
        case 3: return "Note: "+_Note+"      Prüfung: "+_Pruefung;
        default: return "";
      }

   }

public String toString() {
      switch (typ) {
        case 0: return "Prüfung: "+_Pruefung;
        case 1: return "Note: "+_Note;
        case 2: return "Prüfung: "+_Pruefung+"    Note: "+_Note;
        case 3: return "Note: "+_Note+"      Prüfung: "+_Pruefung;
        default: return "";
      }
   }
*/







