All About Choice Class in AWT in Java



The choice class presents a popup menu of choices.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{
//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 void addItem(String item);
public void addNotify();
public int countItems();
public String getItem(int index);
public int getSelectedIndex();
public String getSelectedItem();
protected String paramString()
public void select(int pos);
public void select(String str);
}
 


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.
All About Choice Class in AWT in Java All About Choice Class in AWT in Java Reviewed by Animesh Chatterjee on December 04, 2018 Rating: 5

No comments:

Powered by Blogger.