Choice class is part of Java Abstract Window Toolkit(AWT). The Choice class presents a pop- up menu for the user, the user may select an item from the popup menu. The selected item appears on the top. The Choice class inherits the Component.
Constructor for the Choice class
- Choice() : creates an new empty choice menu .
Different Methods for the Choice Class
| Method | Explanation |
|---|---|
| add(String s) | adds an item to this Choice menu. |
| addItemListener(ItemListener l) | adds the specified item listener to receive item events from this Choice menu. |
| addNotify() | creates the Choice’s peer. |
| getAccessibleContext() | gets the AccessibleContext associated with this Choice. |
| getItem(int i) | gets the string at the specified index in this Choice menu |
| getItemCount() | returns the number of items in this Choice menu. |
| getItemListeners() | returns an array of all the item listeners registered on this choice. |
| getListeners(Class l) | returns an array of all the objects currently registered as FooListeners upon this Choice. |
| getSelectedIndex() | returns the index of the currently selected item. |
| getSelectedItem() | gets a representation of the current choice as a string. |
| insert(String s, int i) | inserts the item into this choice at the specified position. |
| paramString() | returns a string representing the state of this Choice menu. |
| processEvent(AWTEvent e) | processes events on this choice. |
| processItemEvent(ItemEvent e) | processes item events occurring on this Choice menu by dispatching them to any registered ItemListener objects. |
| remove(int p) | removes an item from the choice menu at the specified position. |
| remove(String s) | removes the first occurrence of item from the Choice menu. |
| removeAll() | removes all items from the choice menu. |
| select(int p) | sets the selected item in this Choice menu to be the item at the specified position. |
Below programs illustrate the Choice class in Java AWT:
- Program to create a simple choice and add elements to it:
// Java Program to create a simpleÂ// choice and add elements to it .importjava.awt.*;importjavax.swing.*;ÂÂclasschoice {   Â// choice   ÂstaticChoice c;   Â// frame   ÂstaticJFrame f;   Â// default constructor   Âchoice()   Â{   Â}   Â// Main Method   Âpublicstaticvoidmain(String args[])   Â{       Â// create a frame       Âf =newJFrame("choice");       Â// create e panel       ÂJPanel p =newJPanel();       Â// create a choice       Âc =newChoice();       Â// add element to the list       Âc.add("Andrew");       Âc.add("Arnab");       Âc.add("Ankit");       Â// add choice to panel       Âp.add(c);       Â// add panel to the frame       Âf.add(p);       Â// show the frame       Âf.show();       Âf.setSize(300,300);   Â}}Output :
- Program to create a simple choice and add ItemListener to it:
// Java Program to create a simple// choice and add ItemListener to itimportjava.awt.*;importjavax.swing.*;importjava.awt.event.*;ÂÂclasschoiceimplementsItemListener {   Â// choice   ÂstaticChoice c;   Â// frame   ÂstaticJFrame f;   Â// label   ÂstaticLabel l;   Â// default constructor   Âchoice()   Â{   Â}   Â// Main Method   Âpublicstaticvoidmain(String args[])   Â{       Â// create a frame       Âf =newJFrame("choice");       Â// object       Âchoice ch =newchoice();       Â// create e panel       ÂJPanel p =newJPanel();       Â// create a choice       Âc =newChoice();       Â// add element to the list       Âc.add("Andrew");       Âc.add("Arnab");       Âc.add("Ankit");       Â// add itemListener to it       Âc.addItemListener(ch);       Â// create a label       Âl =newLabel();       Â// set the label text       Âl.setText(c.getSelectedItem() +" selected");       Â// add choice to panel       Âp.add(c);       Âp.add(l);       Â// add panel to the frame       Âf.add(p);       Â// show the frame       Âf.show();       Âf.setSize(300,300);   Â}   Â// if an item is selected   ÂpublicvoiditemStateChanged(ItemEvent e)   Â{       Âl.setText(c.getSelectedItem() +" selected");   Â}}Output :
-
Program to create a choice and manually add elements to it (using add(String s) function):
// Java Program to create a choice and// manually add elements to itÂ// (using add(String s) function)importjava.awt.*;importjavax.swing.*;importjava.awt.event.*;ÂÂclasschoiceimplementsItemListener, ActionListener {   Â// choice   ÂstaticChoice c;   Â// frame   ÂstaticJFrame f;   Â// label   ÂstaticLabel l;   Â// textfield   ÂstaticTextField tf;   Â// default constructor   Âchoice()   Â{   Â}   Â// Main Method   Âpublicstaticvoidmain(String args[])   Â{       Â// create a frame       Âf =newJFrame("choice");       Â// object       Âchoice ch =newchoice();       Â// create e panel       ÂJPanel p =newJPanel();       Â// create a choice       Âc =newChoice();       Â// add element to the list       Âc.add("Andrew");       Â// create a textfield       Âtf =newTextField(7);       Â// create a button       ÂButton b =newButton("ok");       Â// add actionListener       Âb.addActionListener(ch);       Â// add itemListener to it       Âc.addItemListener(ch);       Â// create a label       Âl =newLabel();       ÂLabel l1 =newLabel("add names");       Â// set the label text       Âl.setText(c.getSelectedItem() +" selected");       Â// add choice to panel       Âp.add(c);       Âp.add(l);       Âp.add(l1);       Âp.add(tf);       Âp.add(b);       Â// add panel to the frame       Âf.add(p);       Â// show the frame       Âf.show();       Âf.setSize(250,300);   Â}   Â// if an item is selected   ÂpublicvoiditemStateChanged(ItemEvent e)   Â{       Âl.setText(c.getSelectedItem() +" selected");   Â}   Â// if button is pressed   ÂpublicvoidactionPerformed(ActionEvent e)   Â{       Â// add item to the choice       Âc.add(tf.getText());   Â}}Output :
Note: The programs might not run in an online IDE please use an offline IDE.
Reference : https://docs.oracle.com/javase/7/docs/api/java/awt/Choice.html

