Programming Examples

Are you a Programmer or Application Developer or a DBA? Take a cup of coffee, sit back and spend few minutes here :)

AWT Choice Control Explained

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:

AWT Choice
AWT Choice

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:

Selected Item in the Text
Selected Item in the Text

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:

AWT Choice Control Example
AWT Choice Control Example

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.

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:

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.

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:

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.

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:

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:

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.

4. Complete Code Example

Below is the complete code Example.



5. Watch AWT Choice Control as YouTube

Categories: AWT

Tags: , , , ,

Do you like this Example? Please comment about it for others!!

This site uses Akismet to reduce spam. Learn how your comment data is processed.