Usage

The List class can be used to add a list of items with several visible rows to a container. 

Package

java.awt

Class

java.awt.List

Parent Classes

java.awt.List
java.awt.Component
java.lang.Object

Common Public Constructors

List ()
Creates an empty List instance with the default 4 rows visible.
List (int Rows)
Creates an empty List instance with the specified number of visible rows.
List (int Rows, boolean MultiSelect)
Creates an empty List instance with the specified number of visible rows and the specified MultiSelect switch.

Common Public Methods

void addItemListener (ItemListener Handler)
Specifies an event handler for the list.
void add (String Item)
Inserts the item into this choice at the next position.
void add (String Item, int Index)
Inserts the item into this choice at the specified position.
int getSelectedIndex ()
Returns the index of the selected item.
void setBackground (Color BackgroundColor)
Sets the background color of the list.
void setFont (Font TextFont)
Sets the font for this component.
void setForeground (Color TextColor)
Sets the color of the text for the list.
void Select (int Selection)
Sets the index of the selected item.

Arguments

BackgroundColor
The color to be used for the background of the list.
Index
Position where the item will be inserted.
Item
A String to be listed in the list.
Handler
The object which handles action events from this list.
Multiselect
If true, multiple items can be selected.
Selection
The index of the selected item.
TextColor
The color to be used for the text of the list.
TextFont
The font to be used for the text of the list.

Applet - List2

Source Code - List2.java

import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;

public class List2 extends Applet implements ItemListener {
  /* Declaration */
  private LayoutManager Layout;
  private List Selector;
  private Font SansSerif;

  public List2 () {
    /* Declaration */
    String [] ColorList;
    int i;
          
    /* Instantiation */
    ColorList = new String [9];
    SansSerif = new Font ("SansSerif", Font.BOLD, 14);
    Layout = new FlowLayout ();
    Selector = new List ();
    
    /* Decoration */
    ColorList [0] = "Red";
    ColorList [1] = "Magenta";
    ColorList [2] = "Blue";
    ColorList [3] = "Cyan";
    ColorList [4] = "Green";
    ColorList [5] = "Yellow";
    ColorList [6] = "White";
    ColorList [7] = "Gray";
    ColorList [8] = "Black";
    for (i = 0; i < ColorList.length; ++i) {
      Selector.add (ColorList [i]);
    }
    Selector.setBackground (Color.yellow);
    Selector.setForeground (Color.red);
    Selector.setFont (SansSerif);
            
    /* Location */
    setLayout (Layout);
    add (Selector);
    
    /* Configuration */
    Selector.addItemListener (this);
    
    /* Initialization */
    Selector.select (5);
    setBackground (Color.yellow);
  }

  public void itemStateChanged(ItemEvent e) {
    int Selection;
    Selection = Selector.getSelectedIndex();
    if (Selection == 0) {
      setBackground (Color.red);
    } else if (Selection == 1) {
      setBackground (Color.magenta);
    } else if (Selection == 2) {
      setBackground (Color.blue);
    } else if (Selection == 3) {
      setBackground (Color.cyan);
    } else if (Selection == 4) {
      setBackground (Color.green);
    } else if (Selection == 5) {
      setBackground (Color.yellow);
    } else if (Selection == 6) {
      setBackground (Color.white);
    } else if (Selection == 7) {
      setBackground (Color.gray);
    } else if (Selection == 8) {
      setBackground (Color.black);
    } 
  } 

}

Reference Links

AWT Controls Sampler
Swing JList Examples

External Links

Sun Java Class List API Java 6
Sun Java Class List API Java 5
Sun Java Class List API Java 1.4