Java JComboBox

It provides a combo box through the JComboBox class. A combo box normally displays one entry, but it will also display a drop-down list that allows a user to select a different entry. You can also create a combo box that lets the user enter a selection into the text field.

Example:

JComboBox(Object[ ] items)

JComboBox uses ComboBoxModel. In addition to passing an array of items to be displayed in the drop-down list, items can be dynamically added to the list of choices via the addItem( ) method,

void addItem(Object obj)

Here, obj is the object to be added to the combo box. This method must be used only with mutable combo boxes.
JComboBox generates an action event when the user selects an item from the list. It also generates an item event when the state of selection changes, which occurs when an item is selected or deselected. Thus, changing a selection means that two item events will occur: one for the deselected item and another for the selected item.

Example:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class ComboBox extends JApplet 
{
JLabel jlab;
ImageIcon france, germany, india, japan;
JComboBox jcb;
String flags[] = { "France", "Germany", "India", "Japan" };
public void init() 
{
try 
{
SwingUtilities.invokeAndWait(new Runnable() 
{
public void run() 
{
makeGUI();
}
});
} 
catch(Exception exc) 
{
System.out.println("Can't create because of " + exc);
}
}
private void makeGUI() 
{
setLayout(new FlowLayout());
jcb = new JComboBox(flags);
add(jcb);
jcb.addActionListener(new ActionListener() 
{
public void actionPerformed(ActionEvent ae) 
{
String s = (String) jcb.getSelectedItem();
jlab.setIcon(new ImageIcon(s + ".gif"));
}
});
jlab = new JLabel(new ImageIcon("france.gif"));
add(jlab);
}
}