Thursday, July 25, 2013

Choice

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:
-Choice()
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.
-getItemCount()
counts all text items in the Choice box.
-getSelectedItem()
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.
-removeAll()
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){
setTitle(title);
setLayout(new FlowLayout());
setSize(new Dimension(500,300));

setVisible(true);

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

}
}

Choice in awt

3 comments:

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

    ReplyDelete
  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
    }

    ReplyDelete