Thursday, July 25, 2013


Choice displays a list of items in a box in which only one item can be selected by the user. The Choice can generate item event so the application that wants to perform action when the item of the Choice is selected must implement the ItemListener interface and uses the addItemListener(ItemListener listener) method to register the listener to receive the event.

Constructor of the Choice class:
Useful methods of the Choice class:
-add(String text)
adds text item to the Choice box.
-getItem(int index)
reads the text item at the specified index.
counts all text items in the Choice box.
reads the selected item.
-insert(String text,int index)
inserts text item at the specified index.
-remove(int index)
removes the text item at the specified index.
-remove(String item)
removes the text item that matches the item argument.
removes all items in the Choice box.
-select(int index)
selects the tex item at the specified index.
-select(String text)
slects the tex item that matches the item argument.

In the example code below, the Choice object, clist is created without an item. Later, its add method is used to add a list of numbers with 0 lower bound and 4999 upper bound. One of these numbers will be selected from the Choice, clist and is used as color code when constructing the color object to be used as the background color of the window.

import java.awt.Choice;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.Dimension;
import java.awt.Label;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

class InterfaceShow extends Frame{
InterfaceShow(String title){
setLayout(new FlowLayout());
setSize(new Dimension(500,300));


addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
Label lbl=new Label("Select background color:");
Choice clist=new Choice();
for(int rgb=0;rgb<5000;rgb+=100)
clist.addItemListener(new ItemList());
public void setBGColor(int rgb){
setBackground(new Color(rgb));
class ItemList implements ItemListener{
public void itemStateChanged(ItemEvent e){
public class AWTControls {
public static void main(String[] args){
new InterfaceShow("Using Choice");


Choice in awt


  1. First thank for this tutorial.
    If i have three choices, how can i know which choice is selected?

  2. To know which one of your choice components is selected, you can follow the steps below:
    1. The class that contains the three choice components needs to implement the ItemListener interface to receive the item event.
    2. Register all choice components with item listener by using addItemListener(this) method of the choice.
    3. In the itemStateChanged(ItemEvent e) method, to get the selected choice component you must use the getSource() method of the ItemEvent class.

    public void itemStateChanged(ItemEvent e){
    Choice ch=(Choice)e.getSource();
    //do sth with the selected choice