Java AWT | Choice Class

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 {// choicestaticChoice c;// framestaticJFrame f;// default constructorchoice(){}// Main Methodpublicstaticvoidmain(String args[]){// create a framef =newJFrame("choice");// create e panelJPanel p =newJPanel();// create a choicec =newChoice();// add element to the listc.add("Andrew");c.add("Arnab");c.add("Ankit");// add choice to panelp.add(c);// add panel to the framef.add(p);// show the framef.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 {// choicestaticChoice c;// framestaticJFrame f;// labelstaticLabel l;// default constructorchoice(){}// Main Methodpublicstaticvoidmain(String args[]){// create a framef =newJFrame("choice");// objectchoice ch =newchoice();// create e panelJPanel p =newJPanel();// create a choicec =newChoice();// add element to the listc.add("Andrew");c.add("Arnab");c.add("Ankit");// add itemListener to itc.addItemListener(ch);// create a labell =newLabel();// set the label textl.setText(c.getSelectedItem() +" selected");// add choice to panelp.add(c);p.add(l);// add panel to the framef.add(p);// show the framef.show();f.setSize(300,300);}// if an item is selectedpublicvoiditemStateChanged(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 {// choicestaticChoice c;// framestaticJFrame f;// labelstaticLabel l;// textfieldstaticTextField tf;// default constructorchoice(){}// Main Methodpublicstaticvoidmain(String args[]){// create a framef =newJFrame("choice");// objectchoice ch =newchoice();// create e panelJPanel p =newJPanel();// create a choicec =newChoice();// add element to the listc.add("Andrew");// create a textfieldtf =newTextField(7);// create a buttonButton b =newButton("ok");// add actionListenerb.addActionListener(ch);// add itemListener to itc.addItemListener(ch);// create a labell =newLabel();Label l1 =newLabel("add names");// set the label textl.setText(c.getSelectedItem() +" selected");// add choice to panelp.add(c);p.add(l);p.add(l1);p.add(tf);p.add(b);// add panel to the framef.add(p);// show the framef.show();f.setSize(250,300);}// if an item is selectedpublicvoiditemStateChanged(ItemEvent e){l.setText(c.getSelectedItem() +" selected");}// if button is pressedpublicvoidactionPerformed(ActionEvent e){// add item to the choicec.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




