AWT Choice Class

Advertisements


Introduction

Choice control is used to show pop up menu of choices. Selected choice is shown on the top of the menu.

Class declaration

Following is the declaration for java.awt.Choice class:

public class Choice
   extends Component
      implements ItemSelectable, Accessible

Class constructors

S.N.Constructor & Description
1Choice() ()
Creates a new choice menu.

Class methods

S.N.Method & Description
1void add(String item)
Adds an item to this Choice menu.
2void addItem(String item)
Obsolete as of Java 2 platform v1.1.
3void addItemListener(ItemListener l)
Adds the specified item listener to receive item events from this Choice menu.
4void addNotify()
Creates the Choice's peer.
5int countItems()
Deprecated. As of JDK version 1.1, replaced by getItemCount().
6AccessibleContext getAccessibleContext()
Gets the AccessibleContext associated with this Choice.
7String getItem(int index)
Gets the string at the specified index in this Choice menu.
8int getItemCount()
Returns the number of items in this Choice menu.
9ItemListener[] getItemListeners()
Returns an array of all the item listeners registered on this choice.
10<T extends EventListener> T[] getListeners(Class<T> listenerType)
Returns an array of all the objects currently registered as FooListeners upon this Choice.
11int getSelectedIndex()
Returns the index of the currently selected item.
12String getSelectedItem()
Gets a representation of the current choice as a string.
13Object[] getSelectedObjects()
Returns an array (length 1) containing the currently selected item.
14void insert(String item, int index)
Inserts the item into this choice at the specified position.
15protected String paramString()
Returns a string representing the state of this Choice menu.
16protected void processEvent(AWTEvent e)
Processes events on this choice.
17protected void processItemEvent(ItemEvent e)
Processes item events occurring on this Choice menu by dispatching them to any registered ItemListener objects.
18void remove(int position)
Removes an item from the choice menu at the specified position.
19void remove(String item)
Removes the first occurrence of item from the Choice menu.
20void removeAll()
Removes all items from the choice menu.
21void removeItemListener(ItemListener l)
Removes the specified item listener so that it no longer receives item events from this Choice menu.
22void select(int pos)
Sets the selected item in this Choice menu to be the item at the specified position.
23void select(String str)
Sets the selected item in this Choice menu to be the item whose name is equal to the specified string.

Methods inherited

This class inherits methods from the following classes:

  • java.awt.Component

  • java.lang.Object

Choice Example

Create the following java program using any editor of your choice in say D:/ > AWT > com > tutorialspoint > gui >

AwtControlDemo.java
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

AWT Choice

Advertisements
Advertisements