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

import javax.swing.border.*;

// Beispiel

public class Liste08 extends JFrame {

  Vector items = new Vector();
  JList jlist;

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

  private void setGUI() {
     JButton bnOk;

     JPanel panel;
     JScrollPane scroll;
     myListData data;

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


    for (int i=0; i<20; i++) {
      if ( (i%2) == 0)
        data = new myListData(
        new Font("TimesRoman", Font.PLAIN,16), Color.blue, null, Integer.toHexString(i+1)+" Auswahl" );
      else
        data = new myListData(  new Font("Courier", Font.ITALIC,24), Color.red, null, Integer.toHexString(i+1)+" Auswahl" );
      items.add( data );
    }

    jlist = new JList(items);
    jlist.setBounds(20, 20, 350, 200); // x/y   width height

    jlist.setCellRenderer(
      new myCellRenderer() );

       // Generiere eine Scrollbox, Inhalt Liste
    scroll = new JScrollPane(jlist);
    this.getContentPane().add(scroll, new GridBagConstraints(0, 0, 1, 1, 1.0, 1.0
            ,GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(10,10,0,10), 0, 0));


    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(10,0,10,0), 0, 0));


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

    bnOk.setFont( new Font("Arial", Font.BOLD,18)  );

  } // create

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


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


}


class myCellRenderer implements ListCellRenderer {

  protected DefaultListCellRenderer defaultRenderer;

  private Font theFont=null;
  private Color theForeground=null;
  private Icon theIcon=null;
  private String theText=null;

   public myCellRenderer() {
     defaultRenderer = new DefaultListCellRenderer();
   }


     // Object value ist von Klasse myListdata
   public Component getListCellRendererComponent(JList list, Object value,
          int index, boolean isSelected, boolean cellHasFcous) {

       JLabel label = (JLabel)defaultRenderer.getListCellRendererComponent(
          list, value, index, isSelected, cellHasFcous);

       if ( value instanceof myListData) {
         //
         myListData item = (myListData) value;
         theFont = item.theFont;
         theForeground = item.theForeground;
         theIcon = item.theIcon;
         theText = item.theText;
       }
       else {
         theFont = list.getFont();
         theForeground = list.getForeground();
         theText = "";
       }
       if  (  isSelected ) {
         theFont =  new Font("TimesRoman",
            Font.PLAIN,32);
         theForeground =  Color.yellow;
         label.setForeground(  theForeground );
       }
       else {
         label.setForeground(  theForeground );
       }
       if (theIcon != null) {
         label.setIcon( theIcon );
       }
       label.setText( theText);
       label.setFont( theFont);

       return label;
   }
}


class myListData {
  public Font theFont=null;
  public Color theForeground=null;
  public Icon theIcon=null;
  public String theText=null;

   public myListData(  Font theFont, Color theForeground, Icon theIcon, String theText){
     this.theFont = theFont;
     this.theForeground = theForeground;
     this.theIcon = theIcon;
     this.theText = theText;
   }


   public myListData(  String theText) {
       this(  null, null, null, theText);
   }

}  // myListdata
