Combo Boxes

 

Introduction.  Combo Boxes are typically used when the developer wants the user to select only one of the options in a list and the developer doesn't want the display to take up as much space ass it would with radio button..  For example, say you are filling out a form that requires you to enter the state in which you live.  You don't want to list out fifty radio buttons.  But you can put the states in a combo box that actually takes only one line in a GUI, since it expands only when a user selects it. 

Combo boxes, in Java, are constructed so that the user can only select one of them.  At least in their JListBox variants, they can be configured to the user can make multiple selections.  But, we will focus on single selection combo boxes.

Combo boxes, as they are implemented in Java, also allow for the user to type in their own input if their setEditable(true) property is set.

The following table contains a list of the constructors that are available for the JComboBox class.

 

Constructor Description
JComboBox( ) The constructor is used with no arguments

 

There are also a variety of methods that the developer will need to use in order to configure JComboBoxes aprropriately.  Several of these methods are listed in the following table.

 

Method Description
addItem(object) Used to add objects to the combo box
setEditable(boolean) Determines whether the user can type in their own input.  If this property is not set, then the default is for requiring the user to select from the prescribed list.
getItem(int) This returns the text of the list item at the position given by int.  As with arrays the numbering starts at 0.
getItemCount( ) Returns the number of items in the list.
getSelectedIndex( ) Returns the index position of the currently selected item in the list.
getSelectedItem( ) Returns the text of the currently selected item.
setSelectedIndex(int) This method selects the item at the position specified by the int.
setSelectedIndex(Object) This method selects the specified Object in the list.
setMaximumRowCount(int) This method sets the number of rows that are displayed in the combo box at one time.

 

While the constructor is much less than we have had previously, we now have a much larger variety of built-in methods to work with JComboBoxes.

Again, we will make use of ItemListeners when sensing user inputs relative to our combo boxes.

Displaying What's Selected.  Now we present our first example, an application called SelectItem.java.  This program allows the user to select an entry in a combo box which will cause a display to change.

 

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

public class SelectItem extends JFrame implements ItemListener
{

JTextField result = new JTextField(27);
JComboBox pick = new JComboBox();

public SelectItem( )
{

super("Select Item");
setSize(220, 90);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pick.addItemListener(this);

pick.addItem("Internet Explorer");
pick.addItem("Mozilla");
pick.addItem("Netscape Navigator");
pick.addItem("Opera");
pick.setEditable(false);
result.setHorizontalAlignment(SwingConstants.CENTER);
result.setEditable(false);
JPanel pane = new JPanel( );
pane.setLayout(new GridLayout(2,1));

pane.add(pick);
pane.add(result);

setContentPane(pane);

}

public static void main(String[] arguments)
{

JFrame frame = new SelectItem( );

frame.setVisible(true);

}

public void itemStateChanged(ItemEvent evt)
{

if (evt.getSource( ) == pick)
{

Object newPick = evt.getItem( );
result.setText(newPick.toString( ) + " is the selection.");

}
repaint( );

}

}

 

The following image represents the GUI with the JComboBox selected and expanded.

 

 

The GUI itself, after the selection has been made, follows.

 

 

Notice how the words in the text box change as each option in the combo box is selected.

The code will be discussed in class.  Notice how I have put the ItemListener within the class for Select Item rather than developing its own class for handling the action.

Changing Icons.  We are now going to use a program that I modified from Deitel and Deitel that changes an icon image based on item the user selects. 

You need to copy each of the following images into the directory where you are running the program.

 

Image Name
bug1.gif
bug2.gif
travelbug.gif
buganim.gif

 

You should call the application ComboBoxTest.java.

 

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

public class ComboBoxTest extends JFrame
{

private JComboBox images;
private JLabel label;
private String names[] = {"bug1.gif", "bug2.gif", "travelbug.gif", "buganim.gif"};
private Icon icons[] = {new ImageIcon(names[0]), new ImageIcon(names[1]),new ImageIcon(names[2]),new ImageIcon(names[3])};

public ComboBoxTest( )
{
super("JComboBox Test");

Container c = getContentPane( );
c.setLayout(new GridLayout(2,1));

images= new JComboBox(names);
images.setMaximumRowCount(3);

//  instantiating and registering the ItemListener
//  along with the method to handle the selection events

images.addItemListener(new ItemListener( )
{

public void itemStateChanged(ItemEvent e)
{

label.setIcon(icons[images.getSelectedIndex( )]);

}

}
);

c.add(images);

label = new JLabel(icons[0]);
c.add(label);

setSize(300,125);
show( );

} // ends ComboBoxTest( ) constructor


public static void main(String args[])
{

ComboBoxTest app = new ComboBoxTest( );

app.addWindowListener( new WindowAdapter( )
{

public void windowClosing(WindowEvent e)
{

System.exit(0);

}

}
);

} // ends main( )

} // ends ComboBoxTest class

 

 

The following image represents the GUI.

 

 

Notice how the displayed image changes based on the user selections.