Abstract

Describes the Java Swing JComboBox class, with commonly used methods, and uses a JComboBox in a live applet example, with source code. 

Usage

The JComboBox class can be used to add a drop-down list of items to a container. 

Package

javax.swing

Class

javax.swing.JComboBox

Parent Classes

javax.swing.JComboBox implements Accessible, ActionListener, EventListener, ItemSelectable, ListDataListener
javax.swing.JComponent
java.awt.Container
java.awt.Component implements ImageObserver, MenuContainer, Serializable
java.lang.Object

Common Constructors

JComboBox ()
Creates an empty JComboBox instance. 
JComboBox (Object [] Items)
Creates a JComboBox instance that contains the elements in the specified array. 

Common Methods

void addItemListener (ItemListener Handler)
Configures an event handler for the combo box. 
int getSelectedIndex ()
Returns the index of the selected item. 
void setBackground (Color BackgroundColor)
Sets the background color of the combo box. 
void setFont (Font TextFont)
Sets the font for this component. 
void setForeground (Color TextColor)
Sets the color of the text for the combo box. 
void setSelectedIndex (int Selection)
Sets the index of the selected item. 

Arguments

BackgroundColor
The color to be used for the background of the combo box.
Items
An array of objects to be listed in the combo box.
Handler
The object which handles action events from this combo box.
Selection
The index of the selected item.
TextColor
The color to be used for the text of the combo box.
TextFont
The font to be used for the text of the combo box.

Applet - ComboBox1.class

Source Code - ComboBox1.java

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

public class ComboBox1 extends JApplet implements ItemListener {
  private Container Panel;
  private LayoutManager Layout;
  private JComboBox Selector;
  private String [] ColorList;
  private Font SansSerif;

  public ComboBox1 () {
    /* Instantiation */
    Layout = new FlowLayout ();
    ColorList = new String [9];
    SansSerif = new Font ("SansSerif", Font.BOLD, 14);

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

    Selector = new JComboBox (ColorList);

    /* Location */
    Panel = getContentPane ();
    Panel.setLayout (Layout);
    Panel.add (Selector);
    Panel.setBackground (Color.yellow);
    
    /* Configuration */
    Selector.addItemListener (this);

    /* Decoration */
    Selector.setForeground (Color.red);
    Selector.setFont (SansSerif);
    
    /* Initialization */
    Selector.setSelectedIndex (5);
    Selector.setBackground (Color.yellow);
  }

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

}

Reference Links

Swing Controls Sampler javax.swing.*
AWT Choice Examples java.awt.Choice

External Links

Sun Java site How to Use Combo Boxes
Sun Java site Class JComboBox API Jaba 1.4.2