Java AWT Choice
The object of Choice class is used to show popup menu of choices. Choice selected by user is shown on the top of a menu. It inherits Component class.
AWT Choice Class Declaration
Choice Class constructor
Sr. no. | Constructor | Description |
---|---|---|
1. | Choice() | It constructs a new choice menu. |
Methods inherited by class
The methods of Choice class are inherited by following classes:
- java.awt.Component
- java.lang.Object
Choice Class Methods
Sr. no. | Method name | Description |
---|---|---|
1. | void add(String item) | It adds an item to the choice menu. |
2. | void addItemListener(ItemListener l) | It adds the item listener that receives item events from the choice menu. |
3. | void addNotify() | It creates the peer of choice. |
4. | AccessibleContext getAccessibleContext() | It gets the accessbile context related to the choice. |
5. | String getItem(int index) | It gets the item (string) at the given index position in the choice menu. |
6. | int getItemCount() | It returns the number of items of the choice menu. |
7. | ItemListener[] getItemListeners() | It returns an array of all item listeners registered on choice. |
8. | T[] getListeners(Class listenerType) | Returns an array of all the objects currently registered as FooListeners upon this Choice. |
9. | int getSelectedIndex() | Returns the index of the currently selected item. |
10. | String getSelectedItem() | Gets a representation of the current choice as a string. |
11. | Object[] getSelectedObjects() | Returns an array (length 1) containing the currently selected item. |
12. | void insert(String item, int index) | Inserts the item into this choice at the specified position. |
13. | protected String paramString() | Returns a string representing the state of this Choice menu. |
14. | protected void processEvent(AWTEvent e) | It processes the event on the choice. |
15. | protected void processItemEvent (ItemEvent e) | Processes item events occurring on this Choice menu by dispatching them to any registered ItemListener objects. |
16. | void remove(int position) | It removes an item from the choice menu at the given index position. |
17. | void remove(String item) | It removes the first occurrence of the item from choice menu. |
18. | void removeAll() | It removes all the items from the choice menu. |
19. | void removeItemListener (ItemListener l) | It removes the mentioned item listener. Thus is doesn’t receive item events from the choice menu anymore. |
20. | void select(int pos) | It changes / sets the selected item in the choice menu to the item at given index position. |
21. | void select(String str) | It changes / sets the selected item in the choice menu to the item whose string value is equal to string specified in the argument. |
Java AWT Choice Example
In the following example, we are creating a choice menu using Choice() constructor. Then we add 5 items to the menu using add() method and Then add the choice menu into the Frame.
ChoiceExample1.java
Output:
Java AWT Choice Example with ActionListener
In the following example, we are creating a choice menu with 5 items. Along with that we are creating a button and a label. Here, we are adding an event to the button component using addActionListener(ActionListener a) method i.e. the selected item from the choice menu is displayed on the label when the button is clicked.
ChoiceExample2.java
Output: