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

// Beispiel mit zwei Listen
// Listen durchs Arrays
// Abfrage , Mehrfach Selection

public class Liste03 extends JFrame {

   JList jlist1;
   JList jlist2;
   JButton bnOk1;  // Aktion-Event Links und Rechts
   JButton bnOk2;  // Aktion-Event Links und Rechts
   JButton bnOk3;  // Aktionsschalter Rechts

  //Frame konstruieren
  public Liste03() {
   setSize(620, 320);
   setTitle("JList Liste03");
   setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
   setGUI();
  }

  private void setGUI() {
     JButton bnESC;  // Aktionsschalter Abbruch
     JPanel panel;
     JScrollPane scroll;
     String[] items1;
     String[] items2;


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

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

    jlist1 = new JList(items1);
    jlist1.setSelectionMode(
       ListSelectionModel.SINGLE_INTERVAL_SELECTION );

    scroll = new JScrollPane(jlist1);
    this.getContentPane().add(scroll, new GridBagConstraints(0, 0, 1, 1, 1.0, 1.0
            ,GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(20,20,0,20), 0, 0));


    items2 = new String[20];
    for (int i=0; i<5; i++) {
      items2[i] = Integer.toString(i+1)+" Fachrichtung";
    }

    jlist2 = new JList(items2);
    scroll = new JScrollPane(jlist2);
    this.getContentPane().add(scroll, 
             new GridBagConstraints(1, 0, 1, 1, 1.0, 1.0
            ,GridBagConstraints.CENTER, GridBagConstraints.BOTH, 
              new Insets(20,0,0,20), 0, 0));

    jlist2.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
    /*
    SINGLE_SELECTION
    SINGLE_INTERVAL_SELECTION
    MULTIPLE_INTERVAL_SELECTION
    */

    panel = new JPanel();
    panel.setLayout ( new FlowLayout() );

    bnOk1 = new JButton("Ok (links und rechts)");  // Label erzeugen
    panel.add(bnOk1);

    bnOk2 = new JButton("Ok (links und rechts)");  // Label erzeugen
    panel.add(bnOk2);

    bnOk3 = new JButton("Ok (rechts)");  // Label erzeugen
    panel.add(bnOk3);

    bnESC = new JButton("Abbruch");  // Label erzeugen
    panel.add(bnESC);

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


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

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

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

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

    jlist1.setFont( new Font("Arial", Font.BOLD,12)  );
    jlist2.setFont( new Font("Arial", Font.BOLD,12)  );
    bnOk1.setFont( new Font("Arial", Font.BOLD,12)  );
    bnOk2.setFont( new Font("Arial", Font.BOLD,12)  );
    bnOk3.setFont( new Font("Arial", Font.BOLD,12)  );
    bnESC.setFont( new Font("Arial", Font.BOLD,12)  );


  } // create

   // Check, welcher Eintrag ist gesetzt
  void bnOK_links_rechts_click(ActionEvent e) {
       System.out.println("in links rechts");
       if (e.getSource()==bnOk1) {
         System.out.println("in Bn links");
         checkListe(jlist1);
       }
       if (e.getSource()==bnOk2) {
         System.out.println("in Bn rechts");
         checkListe(jlist2);
       }
  }

  private void checkListe(JList jlist) {
     String sResult;
     Object[] elements = jlist.getSelectedValues();

     if (elements.length==0) {
       System.out.println("Kein Eintrag selektiert");
     }
     else {
       for (int i=0; i<elements.length; i++) {
         sResult = (String) elements[i];  // Typecasting
         System.out.println("Auswahl: "+sResult);
       }
       System.out.println("");
       System.out.println("");
     }
  }

   // Check, welcher Eintrag ist gesetzt, mehrere möglich
  void bnOK_rechts_click(ActionEvent e) {
     String sResult;
     Object[] elements = jlist2.getSelectedValues();

     if (elements.length==0) {
       System.out.println("Kein Eintrag selektiert");
     }
     else {
       for (int i=0; i<elements.length; i++) {
         sResult = (String) elements[i];  // Typecasting
         System.out.println("Auswahl: "+sResult);
       }
       System.out.println("");
       System.out.println("");
     }
  }


  void bnOK_rechts_singleSelect_click(ActionEvent e) {
     String sResult;
     int i =  jlist2.getSelectedIndex();
       System.out.println("i :"+i);
     sResult = (String) jlist2.getSelectedValue();
     if (sResult==null) {
       System.out.println("Kein Eintrag selektiert");
     }
     else {
       System.out.println(sResult);
       System.out.println(" ");
       System.out.println(" ");
     }
  }

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


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


}
