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

import javax.swing.border.*;

// Beispiel mit CellRenderer

public class Liste07 extends JFrame {

  Vector items = new Vector();
  JList jlist;

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

  private void setGUI() {
     JButton bnOk;

     JPanel panel;
     JScrollPane scroll;

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

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

    jlist = new JList(items);
    jlist.setCellRenderer(
      new myCellRenderer() );

    scroll = new JScrollPane(jlist);

       // Erzeuge eine Scrollbox, Inhalt Liste
    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);
      }
    });
    jlist.setFont( new Font("Arial", Font.BOLD,18)  );
    bnOk.setFont( new Font("Arial", Font.BOLD,18)  );

  } // create


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



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


}


class myCellRenderer implements ListCellRenderer {

  protected Border noFocusBorder;
  protected TitledBorder focusBorder;
  protected DefaultListCellRenderer defaultRenderer;

   public myCellRenderer() {
     noFocusBorder = new EmptyBorder(15,1,1,1);
     focusBorder   = new TitledBorder(
        LineBorder.createGrayLineBorder(), "Focused");
     defaultRenderer = new DefaultListCellRenderer();
   }


   public Component getListCellRendererComponent(
          JList list, Object value,
          int index, boolean isSelected, boolean cellHasFocus) {
       JLabel label = (JLabel)defaultRenderer.
             getListCellRendererComponent(
          list, value, index, isSelected, cellHasFocus);

       if  ( cellHasFocus ) {
         label.setBorder( focusBorder);
         label.setBackground(  Color.red);
       }
       else {
         label.setBorder( noFocusBorder);
         label.setBackground(
            new Color(0,200,230) );
       }
       return label;
   }
}
