1. About AWT Choice Control
The AWT Choice Control lists items with a drop-down arrow. User can pick any item from it. The below picture shows the AWT Choice Control:

Here, the control is displaying five items and the user can pick any one item. To display the item, we need to click the down arrow key and after picking the item, the drop-down list collapses and shows the selected item in the text part of the control. The below picture shows, user selected the item Indonesia from the drop-down list of the Choice Control:

Note, when there are a greater number of items in the Choice Control, the Drop-Down List will display a scroll bar. In the above Choice Control, AWT does not place the scroll bars. The Choice Control produces ItemEvent when the user picks an item from the Drop-Down List.
2. About AWT Choice Example
Now let us look at the example which we are going to create. Have a look at the AWT Frame below:

Top portion of the Frame window has the AWT Choice Control. There are two Text fields in the Frame. The first one takes a string and adds it to the Choice Control via Add button click. The next text field is used to display the selected text of the Choice Control. Moreover, the Remove button click will use this text field to delete an item in the Choice.
3. Coding the Example
In this example, we will use two listeners. One is to handle the Add and Remove button clicks and other one to handle the selection change of the Choice Control. Now, we will start our coding.
3.1 Prepare Middle Panel
3.1.1 Class Members
First, we add three class members for the AWT Frame derived class. We keep the below AWT Components as a member variable so that we can access them in the Event Listener methods.
1 2 3 4 |
//Sample 01: Members private Choice choice; private TextField txtAdd; private TextField txtRemove; |
3.1.2 Create Middle Panel
Next, we create a Panel
and set its layout as FlowLayout
. Then we create the TextField
Controls and AWT Buttons
and add it to the Panel
Container. Note, we disable the text field for remove action. Later, we will populate this text field whenever the selection in the Choice Control changes. The remove button click depends on this read only text field as it finds the item in the Choice Control for sure. Below is the code:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
//Sample 02: Supporting Controls for this Example //2.1 Create a Panel Panel MidPane = new Panel(); MidPane.setLayout(new FlowLayout()); //2.2 To Add a String txtAdd = new TextField(30); Button btnAdd = new Button("Add"); //2.3 To remove a String txtRemove = new TextField(30); txtRemove.setEnabled(false); Button btnRemove = new Button("Remove"); |
3.1.3 Register For Add & Remove Button Events
We register the Add and Remove buttons with the ActionListener
to manage the click event of them. Also note the usage of setActionCommand
. Here, we set to actions as Add, Remove. These action commands are useful to know the event source inside the common handler function.
1 2 3 4 5 |
//Sample 04: Register to Listener, Set Action Cmd btnAdd.addActionListener(this); btnRemove.addActionListener(this); btnAdd.setActionCommand("Add"); btnRemove.setActionCommand("Remove"); |
Since we pass ‘this
’ reference to the addActionListener
method, we implement the ActionListener
interface for our AWT Frame derived class FrameWindow
. Below is the code:
1 2 3 4 |
//Sample 03: Implement Action Listener public class FrameWindow extends Frame implements WindowListener, ActionListener { |
3.1.4 Add Controls to Middle Panel
Finally, we make our Middle Panel ready by adding buttons and text fields to it via the add
method. We add this middle to the AWT Frame window using the add method of the Content Pane retrieved from the AWT Frame
.
1 2 3 4 5 6 |
//Sample 05: Add the Controls MidPane.add(txtAdd); MidPane.add(btnAdd); MidPane.add(txtRemove); MidPane.add(btnRemove); add(BorderLayout.CENTER,MidPane); |
3.2 Prepare AWT Choice Control
After creating the AWT Choice Control, we use the add method to add String as display items. In the below code, we add five strings to the Control:
1 2 3 4 5 6 7 8 |
//Sample 06: Create Choice Control choice = new Choice(); choice.add("France"); choice.add("Germany"); choice.add("Indonesia"); choice.add("India"); choice.add("United Kingdom"); add(BorderLayout.NORTH, choice); |
Next, the code will register the
Choice Control with the ItemListener
and receive ItemEvent
when use selects an item from the Choice Control. Below is the code snippet:
1 2 3 4 5 6 7 |
//Sample 10: Register Choice for ItemEvent choice.addItemListener(this); //Sample 03: Implement Action Listener //Sample 08: Implement Item Listener for Choice public class FrameWindow extends Frame implements WindowListener, ActionListener, ItemListener |
3.3 Add and Remove Item on AWT Choice Control
The AWT
Choice Control will produce ItemEvent
when the user changes the choice. From the ItemEvent
, we can know the event source using the
getItemSelectable method. The
getSelectedItem method will give the item which is selected by the user.
In our case, we have only one UI part which produces this event. It is our AWT Choice
Control. Despite that, for double check, we form an if condition before calling the
getSelectedItem method. Note, now we can test the
remove method by clicking the remove button. Before doing this step, we must first select an item from the AWT Choice Control.
1 2 3 4 5 6 7 8 |
@Override public void itemStateChanged(ItemEvent e) { //Sample 09: Display the Selected String if (e.getItemSelectable() == choice) { txtRemove.setText(choice.getSelectedItem()); } } |
4. Complete Code Example
Below is the complete code Example.
4.1 MainEntryAwt.java
1 2 3 4 5 6 7 8 9 10 11 |
package AwtDemoPkg; import java.awt.Frame; public class MainEntryAwt { public static void main(String[] args) { //Sample 03: Make the FrameWindow Visible FrameWindow fw = new FrameWindow("AWT Choice Control"); fw.setVisible(true); } } |
4.2 FrameWindow.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 |
package AwtDemoPkg; import java.awt.BorderLayout; import java.awt.Button; import java.awt.Choice; import java.awt.FlowLayout; import java.awt.Frame; import java.awt.GridBagConstraints; import java.awt.GridBagLayout; import java.awt.Label; import java.awt.Panel; import java.awt.TextField; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.ItemEvent; import java.awt.event.ItemListener; import java.awt.event.WindowEvent; import java.awt.event.WindowListener; //Sample 03: Implement Action Listener //Sample 08: Implement Item Listener for Choice public class FrameWindow extends Frame implements WindowListener, ActionListener, ItemListener { //Sample 01: Members private Choice choice; private TextField txtAdd; private TextField txtRemove; public FrameWindow(String FrameTitle) { //Display the Frame Window super(FrameTitle); setSize(500, 200); setLocation(100,100); addWindowListener(this); //Sample 02: Supporting Controls for this Example //2.1 Create a Panel Panel MidPane = new Panel(); MidPane.setLayout(new FlowLayout()); //2.2 To Add a String txtAdd = new TextField(30); Button btnAdd = new Button("Add"); //2.3 To remove a String txtRemove = new TextField(30); txtRemove.setEnabled(false); Button btnRemove = new Button("Remove"); //Sample 04: Register to Listener, Set Action Cmd btnAdd.addActionListener(this); btnRemove.addActionListener(this); btnAdd.setActionCommand("Add"); btnRemove.setActionCommand("Remove"); //Sample 05: Add the Controls MidPane.add(txtAdd); MidPane.add(btnAdd); MidPane.add(txtRemove); MidPane.add(btnRemove); add(BorderLayout.CENTER,MidPane); //Sample 06: Create Choice Control choice = new Choice(); choice.add("France"); choice.add("Germany"); choice.add("Indonesia"); choice.add("India"); choice.add("United Kingdom"); add(BorderLayout.NORTH, choice); //Sample 10: Register Choice for ItemEvent choice.addItemListener(this); } public void windowOpened(WindowEvent e) {} public void windowClosed(WindowEvent e) {} public void windowIconified(WindowEvent e) {} public void windowDeiconified(WindowEvent e) {} public void windowActivated(WindowEvent e) {} public void windowDeactivated(WindowEvent e) {} public void windowClosing(WindowEvent e) { this.dispose(); } @Override public void actionPerformed(ActionEvent e) { //Sample 07: Implement Button Click Actions String ActionCommand = e.getActionCommand(); switch (ActionCommand) { case "Add": choice.add(txtAdd.getText()); break; case "Remove": String RemoveStr = txtRemove.getText(); if (RemoveStr.length() > 0) choice.remove(txtRemove.getText()); break; } } @Override public void itemStateChanged(ItemEvent e) { //Sample 09: Display the Selected String if (e.getItemSelectable() == choice) { txtRemove.setText(choice.getSelectedItem()); } } } |
5. Watch AWT Choice Control as YouTube
Categories: AWT
Tags: add, AWT Choice, ItemEvent, ItemListener, remove