Java AWT (Abstract Window Toolkit) Choice is a class in the Java programming language that provides a compact menu of choices. It is used to create a drop-down list of items from which the user can choose one item.
Here is an example of how to create a Choice in Java AWT:
import java.awt.Choice; import java.awt.Frame; public class MyFrame extends Frame { public MyFrame() { // Create a new Choice Choice choice = new Choice(); // Add some items to the Choice choice.add("Item 1"); choice.add("Item 2"); choice.add("Item 3"); // Add the Choice to the Frame add(choice); // Set the properties of the Frame setTitle("Java AWT Choice Example"); setSize(300, 200); setVisible(true); } public static void main(String[] args) { new MyFrame(); } }
In the example above, we create a new Choice, add some items to it, and then add the Choice to a Frame. When we run the program, we will see a Frame with a drop-down list containing the three items we added to the Choice. When the user selects an item from the list, we can use the getSelectedItem()
method of the Choice class to get the selected item.
AWT Choice Class Declaration:
The AWT Choice class is declared as follows:
public class Choice extends Component implements ItemSelectable, Accessible
The Choice
class extends the Component
class and implements two interfaces – ItemSelectable
and Accessible
.
Here is a brief explanation of each of these:
Component
: This is the base class for all AWT components. It provides methods for managing the size, position, and visibility of the component.ItemSelectable
: This interface is used to indicate that an object can be selected. TheChoice
class implements this interface to allow items in the list to be selected.Accessible
: This interface is used to provide accessibility information for a component. TheChoice
class implements this interface to allow assistive technologies to interact with the component.
Choice Class constructor:
The Choice
class in AWT provides a default constructor that can be used to create a new Choice object. The syntax for the constructor is as follows:
public Choice()
This constructor creates a new Choice object with an empty list of items.
Here’s an example of how to create a new Choice object using the default constructor:
Choice choice = new Choice();
This will create a new Choice object called choice
. The list of items in the choice will be empty until items are added using the add()
method.
Alternatively, the Choice
class also provides a constructor that allows you to initialize the list of items when creating the Choice
object. The syntax for this constructor is as follows:
public Choice(String[] items)
Here, the items
parameter is an array of String
objects that will be added to the choice. For example:
String[] choices = {"Red", "Green", "Blue"}; Choice colorChoice = new Choice(choices);
This will create a new Choice object called colorChoice
with three items – “Red”, “Green”, and “Blue”.
Methods inherited by class:
The Choice
class in AWT inherits several methods from its superclass, Component
. Here are some of the commonly used inherited methods:
void setFont(Font font)
: Sets the font used to display the text in the Choice component.void setSize(int width, int height)
: Sets the size of the Choice component.void setLocation(int x, int y)
: Sets the location of the Choice component.void setVisible(boolean visible)
: Sets the visibility of the Choice component.
In addition to the inherited methods, the Choice
class also provides its own set of methods for managing the list of items in the Choice component. Here are some of the commonly used methods:
void add(String item)
: Adds an item to the list of choices in the Choice component.void add(String item, int index)
: Adds an item to the list of choices in the Choice component at a specified index.void remove(String item)
: Removes an item from the list of choices in the Choice component.int getItemCount()
: Returns the number of items in the list of choices in the Choice component.String getItem(int index)
: Returns the item at the specified index in the list of choices in the Choice component.String getSelectedItem()
: Returns the currently selected item in the list of choices in the Choice component.
These methods allow you to add and remove items from the list of choices in the Choice component, as well as retrieve information about the items in the list and the currently selected item.
Choice Class Methods:
The Choice
class in AWT provides several methods to manage the list of items in the choice component. Here are some of the commonly used methods:
void add(String item)
: Adds an item to the end of the list of choices in the Choice component.void add(String item, int index)
: Inserts an item into the list of choices at the specified index.void remove(String item)
: Removes the specified item from the list of choices in the Choice component.void removeAll()
: Removes all items from the list of choices in the Choice component.int getItemCount()
: Returns the number of items in the list of choices in the Choice component.String getItem(int index)
: Returns the item at the specified index in the list of choices in the Choice component.int getSelectedIndex()
: Returns the index of the currently selected item in the list of choices in the Choice component.String getSelectedItem()
: Returns the currently selected item in the list of choices in the Choice component.void select(int index)
: Selects the item at the specified index in the list of choices in the Choice component.void select(String item)
: Selects the specified item in the list of choices in the Choice component.
These methods allow you to add and remove items from the list of choices in the Choice component, retrieve information about the items in the list and the currently selected item, and select a particular item in the list.
Java AWT Choice Example:
Here is an example of how to use the Choice
class in Java AWT:
import java.awt.Choice; import java.awt.Frame; import java.awt.Label; import java.awt.event.ItemEvent; import java.awt.event.ItemListener; public class ChoiceExample extends Frame implements ItemListener { private Choice colorChoice; private Label label; public ChoiceExample() { setTitle("Choice Example"); // Create a Choice component colorChoice = new Choice(); colorChoice.add("Red"); colorChoice.add("Green"); colorChoice.add("Blue"); colorChoice.addItemListener(this); // Create a Label component label = new Label("Choose a color:"); // Add components to the frame add(label); add(colorChoice); // Set the size and visibility of the frame setSize(200, 100); setVisible(true); } public void itemStateChanged(ItemEvent e) { // Get the selected item from the Choice component String color = colorChoice.getSelectedItem(); // Update the Label component with the selected color label.setText("You chose " + color); } public static void main(String[] args) { ChoiceExample example = new ChoiceExample(); } }
In this example, we create a new ChoiceExample
class that extends the Frame
class and implements the ItemListener
interface. We create a Choice
component called colorChoice
with three items – “Red”, “Green”, and “Blue”. We add an ItemListener
to the colorChoice
component so that we can detect when the user selects a new color. We also create a Label
component called label
with some initial text.
In the itemStateChanged()
method, we get the currently selected item from the colorChoice
component and update the text of the label
component to display the selected color.
Finally, we create an instance of the ChoiceExample
class and set its size and visibility. When the user selects a new color from the colorChoice
component, the text of the label
component will update to display the selected color.
Java AWT Choice Example with ActionListener:
Here is an example of how to use the Choice
class in Java AWT with an ActionListener
:
import java.awt.Choice; import java.awt.Frame; import java.awt.Label; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; public class ChoiceExample implements ActionListener { private Frame frame; private Choice colorChoice; private Label label; public ChoiceExample() { frame = new Frame("Choice Example"); // Create a Choice component colorChoice = new Choice(); colorChoice.add("Red"); colorChoice.add("Green"); colorChoice.add("Blue"); colorChoice.addActionListener(this); // Create a Label component label = new Label("Choose a color:"); // Add components to the frame frame.add(label); frame.add(colorChoice); // Set the size and visibility of the frame frame.setSize(200, 100); frame.setVisible(true); } public void actionPerformed(ActionEvent e) { // Get the selected item from the Choice component String color = colorChoice.getSelectedItem(); // Update the Label component with the selected color label.setText("You chose " + color); } public static void main(String[] args) { ChoiceExample example = new ChoiceExample(); } }
In this example, we create a new ChoiceExample
class that uses an ActionListener
instead of an ItemListener
. We create a Frame
object called frame
, a Choice
object called colorChoice
, and a Label
object called label
. We add the colorChoice
and label
components to the frame
.
In the constructor, we add an ActionListener
to the colorChoice
component. When the user selects a new color, the actionPerformed()
method is called. In this method, we get the currently selected item from the colorChoice
component and update the text of the label
component to display the selected color.
Finally, we create an instance of the ChoiceExample
class and set the size and visibility of the frame
. When the user selects a new color from the colorChoice
component, the text of the label
component will update to display the selected color.