AWT Choice Class

Choice control is used to show pop up menu of choices. Selected choice is shown on the top of the menu.
Following is the declaration for java.awt.Choice class:
public class Choice
   extends Component
      implements ItemSelectable, Accessible
Choice() () 
Creates a new choice menu.
void add(String item) 
Adds an item to this Choice menu.
void addItem(String item) 
Obsolete as of Java 2 platform v1.1.
void addItemListener(ItemListener l) 
Adds the specified item listener to receive item events from this Choice menu.
void addNotify() 
Creates the Choice's peer.
int countItems() 
Deprecated. As of JDK version 1.1, replaced by getItemCount().
AccessibleContext getAccessibleContext() 
Gets the AccessibleContext associated with this Choice.
String	getItem(int index) 
Gets the string at the specified index in this Choice menu.
int getItemCount() 
Returns the number of items in this Choice menu.
ItemListener[]	getItemListeners() 
Returns an array of all the item listeners registered on this choice.
<T extends EventListener> T[] getListeners(Class<T> listenerType) 
Returns an array of all the objects currently registered as FooListeners upon this Choice.
int getSelectedIndex() 
Returns the index of the currently selected item.
String	getSelectedItem() 
Gets a representation of the current choice as a string.
Object[] getSelectedObjects() 
Returns an array (length 1) containing the currently selected item.
void insert(String item, int index) 
Inserts the item into this choice at the specified position.
protected String paramString()
Returns a string representing the state of this Choice menu.
protected void processEvent(AWTEvent e) 
Processes events on this choice.
protected void processItemEvent(ItemEvent e) 
Processes item events occurring on this Choice menu by dispatching them to any registered ItemListener objects.
void remove(int position) 
Removes an item from the choice menu at the specified position.
void remove(String item) 
Removes the first occurrence of item from the Choice menu.
void removeAll() 
Removes all items from the choice menu.
void removeItemListener(ItemListener l) 
Removes the specified item listener so that it no longer receives item events from this Choice menu.
void select(int pos) 
Sets the selected item in this Choice menu to be the item at the specified position.
void select(String str) 
Sets the selected item in this Choice menu to be the item whose name is equal to the specified string.
This class inherits methods from the following classes:
java.awt.Component
java.awt.Component
java.lang.Object
java.lang.Object
Create the following java program using any editor of your choice in say D:/ > AWT > com > tutorialspoint > gui >
package com.tutorialspoint.gui;

import java.awt.*;
import java.awt.event.*;

public class AwtControlDemo {

   private Frame mainFrame;
   private Label headerLabel;
   private Label statusLabel;
   private Panel controlPanel;

   public AwtControlDemo(){
      prepareGUI();
   }

   public static void main(String[] args){
      AwtControlDemo  awtControlDemo = new AwtControlDemo();
      awtControlDemo.showChoiceDemo();
   }

   private void prepareGUI(){
      mainFrame = new Frame("Java AWT Examples");
      mainFrame.setSize(400,400);
      mainFrame.setLayout(new GridLayout(3, 1));
      mainFrame.addWindowListener(new WindowAdapter() {
         public void windowClosing(WindowEvent windowEvent){
            System.exit(0);
         }        
      });    
      headerLabel = new Label();
      headerLabel.setAlignment(Label.CENTER);
      statusLabel = new Label();        
      statusLabel.setAlignment(Label.CENTER);
      statusLabel.setSize(350,100);

      controlPanel = new Panel();
      controlPanel.setLayout(new FlowLayout());

      mainFrame.add(headerLabel);
      mainFrame.add(controlPanel);
      mainFrame.add(statusLabel);
      mainFrame.setVisible(true);  
   }

   private void showChoiceDemo(){                                       

      headerLabel.setText("Control in action: Choice"); 
      final Choice fruitChoice = new Choice();

      fruitChoice.add("Apple");
      fruitChoice.add("Grapes");
      fruitChoice.add("Mango");
      fruitChoice.add("Peer");

      Button showButton = new Button("Show");

      showButton.addActionListener(new ActionListener() {
         public void actionPerformed(ActionEvent e) {     
            String data = "Fruit Selected: " 
            + fruitChoice.getItem(fruitChoice.getSelectedIndex());
            statusLabel.setText(data);
         }
      }); 

      controlPanel.add(fruitChoice);
      controlPanel.add(showButton);

      mainFrame.setVisible(true);  
   }
}
Compile the program using command prompt. Go to D:/ > AWT  and type the following command.
D:\AWT>javac com\tutorialspoint\gui\AwtControlDemo.java
If no error comes that means compilation is successful. Run the program using following command.
D:\AWT>java com.tutorialspoint.gui.AwtControlDemo
Verify the following output

    13 Lectures 
     2 hours 

 EduOLC
 Print
 Add Notes
 Bookmark this page