All About Choice Class in AWT in Java

0
33
Spread the love
  • 1
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
    1
    Share

Choice Class in Java
Choice Class in Java 

Choice Class in Java :

The choice class presents a popup menu of choices or option menu or drop down list.The current choice is displayed as the title of the menu.After any choice is made, AWT sends an action event to the choice menu.The event’s target is the choice menu and it’s object is the String label of the currently selected item. . The Choicebox is a single line window with a dropdown arrow revealing a dropdown Listbox.it has the same constructor and methods as the Listbox.The events for a choicebox are slightly different selecting an item from a Choicebox.It generates an action performed event,while clicking on an item in a List box,generates an itemStateChanged event.The choicebox is not a window style combo box where we can type in or select from the top line.But we could easily construct such a combo box from a textfield,a button and a hidden listbox.An application should override the action method of the choice menu or one of it’s parent containers in order to cause some action to occur.

The class structure of Choice class is given as:


public class java.awt.Choice extends 
             java.awt.Component implements
             java.awt.ItemSelectable{
//constructors:
public Choice();//Creates a new Choice menu.The menu initially has no item in it.By default
//the first item added the Choice menu becomes the selected item,until a different selection
//is made by the user or by calling one of the select methods.
//methods:
public synchronized void add(String item);
public synchronized void addItem(String item);
public synchronized void addItemListener(ItemListener lis); 
public void addNotify();
public int countItems();
public int getItemCount();
public String getItem(int index);
public int getSelectedIndex();
public synchronized String getSelectedItem();
protected String paramString()
public synchronized void select(int pos);
public synchronized void select(String str);
public synchronized Object[] getSelectedObjects(); 
public synchronized void insert(String item, int index);
public synchronized void remove(String item);
public synchronized void remove(int position);
public synchronized void removeAll();
public synchronized void removeItemListener(ItemListener lis);
protected void processEvent(AWTEvent e); 
protected void processItemEvent(ItemEvent e);
}
 

An example:-


Choice os=new Choice();
os.addItem("A");
os.addItem("B");
os.addItem("C");
//method related to Applet
int width=Integer.parseInt(getParameter("width"));
int height=Integer.parseInt(getParameter("height"));
//method supported:
reshape(int x,int y,int x1,int y1);
getSelectedItem();//returns the String of the item itself.
getSelectedIndex();//returns the index of the item itself.
select(int pos);//selects the item by index position.
select(String name);//selects the item by name.
addItem(String item);//adds an item to the choice.
 

The items of a choice will be left justified.

Don't miss out!
Subscribe To Newsletter

Receive top technical news, lesson ideas, travel tips and more!

Invalid email address
Give it a try. You can unsubscribe at any time.

Spread the love
  • 1
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
    1
    Share

LEAVE A REPLY

Please enter your comment!
Please enter your name here