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

import java.util.Vector;

// Beispiel einem Vector
// Schalter delete, insert, edit
  // NL = '\n'

public class Liste06 extends JFrame {


  Vector items = new Vector();
  JList jlist;

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

  private void setGUI() {
     JButton bnDelete;
     JButton bnInsert;
     JButton bnEdit;
     JButton bnESC;  // Aktionsschalter Abbruch

     JPanel panel;
     JScrollPane scroll;

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

    for (int i=0; i<10; i++) {
      items.add( Integer.toHexString(i+1)+" Auswahl" );
    }
//      items.setSize(100);

    jlist = new JList(items);
    jlist.setBounds(20, 20, 350, 200); // x/y   width height
       // Generiere eine Scrollbox, Inhalt Liste
    scroll = new JScrollPane(jlist);

    //  items.add( "ddddddddd" );

    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));



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

    bnInsert = new JButton("Insert");  // Label erzeugen
    panel.add(bnInsert);

    bnDelete = new JButton("Delete");  // Label erzeugen
    panel.add(bnDelete);

    bnEdit = new JButton("Edit");  // Label erzeugen
    panel.add(bnEdit);

    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));

            //-------------

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

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

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

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

    jlist.setFont( new Font("Arial", Font.BOLD,18)  );
    bnInsert.setFont( new Font("Arial", Font.BOLD,18)  );
    bnDelete.setFont( new Font("Arial", Font.BOLD,18)  );
    bnEdit.setFont( new Font("Arial", Font.BOLD,18)  );
    bnESC.setFont( new Font("Arial", Font.BOLD,18)  );

  } // create


  void bnInsert_click(ActionEvent e) {
   String sNew;
   sNew = CBasis.getInput( "U-Nummer",
       "Eingabe eines neuen Studenten",
           "u" ) ;
         System.out.println(sNew);
   if (sNew!=null) {
     items.add( new String(sNew) );
       // Probleme, wenn keine Datensätze vorher gelöscht wurden
       // deshalb komplett neues Update des Vectors
     // jlist.setListData(items);
     jlist.updateUI();// Neuzeichnen
   }
  } // insert



  void bnDelete_click(ActionEvent e) {
    Object obj;
    obj = jlist.getSelectedValue();
    //int index1 = jlist.getSelectedIndex();

    if (obj!=null) {
        //  '\n' nur bei Message
      String sRes = (String) obj;  // Casting
      if ( CBasis.YesNo("Eintrag löschen",
        "Wollen Sie wirklich den Eintrag löschen?  "+
         sRes) ) {
        int index = jlist.getSelectedIndex();
        items.remove(index);
        //liste.setListData(items);
        jlist.updateUI();
      }
    }
  }

  void bnEdit_click(ActionEvent e) {
    Object obj;
    obj = jlist.getSelectedValue();

    if (obj==null) {
      return; // kein Elelement ausgewählt
    }
    else {
        //  '\n' nur bei Message
      String sRes = (String) obj;  // Casting
     sRes = CBasis.getInput( "U-Nummer",
     "Eingabe eines neuen Studenten",sRes ) ;
         System.out.println(sRes);
     if (sRes!=null) {
        int index = jlist.getSelectedIndex();
        items.setElementAt(sRes,index);
        //liste.setListData(items);
        jlist.updateUI();
     }
   }
  }  // edit

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





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


}

class CBasis {

    // Eingabe eines String
  static public String getInput( String sTitle,
          String sMessage, String sInput) {
    return (String) JOptionPane.showInputDialog(null,
         sTitle, sMessage,
      JOptionPane.PLAIN_MESSAGE,
      null, null, sInput);
  } //






  static public boolean YesNo( String sTitle, String sMessage) {
    int retcode;
    retcode = JOptionPane.showConfirmDialog(null,sMessage,sTitle,
      JOptionPane.YES_NO_OPTION,
      JOptionPane.QUESTION_MESSAGE);  // Symbol

    switch (retcode) {
       case JOptionPane.YES_OPTION:
         // ja, Änderungen speichern
         return true;
       case JOptionPane.NO_OPTION:
         return false;
       case JOptionPane.CANCEL_OPTION:
       default:
         // abbrechen
         return false;
    }
  } // YesNo

}