Choice Boxes

The Choice class is a lot like lists but allows you to conserve space since it provides a pop-up menu of text string choices. The menu title displays the current choice. To work with a choice box, an instance of the Choice class must be created.

Choice theoptions = new Choice();

Once you have created the choice, the addItem method enables you to add new entries.

theoptions.addItem("Red");
theoptions.addItem("Green");

The currently selected item can be changed by name or index.

theoption.select("Red");
OR
theoption.select(0);

The getSelectedIndex and getSelectedItem methods would return the position of the selected item and string name of the selected item respectively.

The Choice class calls the action method whenever a choice is made, regardless of whether it is a new choice or not. The parameter whatAction contains the name of the selected item. The following example shows how a choice box can be used to change the background color of an applet window.

 

The code for the Applet is listed below

import java.applet.*;
import java.awt.*;



public class choices extends Applet
{
String currentColor;
Choice theoptions = new Choice();

  public void init()
	{
	  add(new Label("The choice
	    box allows easy selection"));
	  add(new Label("of items and
	    conserves space."));
	  add(new Label("You can make the
	    choices blend with labels"));
	  add(new Label("and produce smooth
	    flowing interaction."));
	  add(new Label("Like the one below
	    this label."));	
	  add(new Label("I want the screen
	    color to be"));
	  theoptions.addItem("Red");
	  theoptions.addItem("Green");
	  theoptions.addItem("Blue");
  	  theoptions.addItem("White");
	  theoptions.addItem("Cyan");
	  theoptions.addItem("Yellow");
	  add(theoptions);	 
	}
We import all the facilities of the AWT and applet that Java has to offer by using the * wildcard.

Create an applet named choices.

Declare a string variable to hold the current color.
Create a choice box.

In the init method, add labels to the applet window. Also add items of color to the choice box. Then add the filled choice box to the applet window.


 public boolean action(Event evt, Object
   whichAction)
{

   if (!(evt.target instanceof Choice))
   {
      return false;
   }
   Choice whichChoice = (Choice) evt.target;
      if (whichChoice == theoptions)
      {
        currentColor = (String) whichAction;
        repaint();
        return true;
      }
   return false;
	 
}
When an event takes place the default action method calls the event method of our component. The whichAction parameter of the action method will be an instance of a Boolean class that is true if the choice was selected and false if the choice was unselected.

The currentColor stores the string returned by the action method which is the color that was selected. The repaint method is called next.
public void paint(Graphics g)
{
   if (currentColor=="Red")
      setBackground(Color.red);
   if (currentColor=="White")
      setBackground(Color.white);
   if (currentColor=="Blue")
      setBackground(Color.blue);  
   if (currentColor=="Green")
      setBackground(Color.green);
   if (currentColor=="Cyan")
      setBackground(Color.cyan);
   if (currentColor=="Yellow")
      setBackground(Color.yellow);  
   }	   	
}
This method paints the applet window the desired color.

The easiest way to obtain the code for this Applet is to click here and copy the contents of the screen that loads in your browser , paste it in a text editor and save it in a file called "choices.java".

After compiling this program we need to create an HTML page with a reference to the class file. It will require a graphic window that is large enough to contain the two lists, buttons, and label (width-251 and height-351).

Save the HTML code in the same directory as the class file.

When you load the HTML file in your browser the applet will execute as below.

Back to the Java AWT Components Page

Back to the Table of Contents