AWT MULTI-SELECT LIST Control

Java AWT Multi-Select List Example

1. AWT Multi-Select List Control

AWT Multi-Select List Control allows adding a list of items in it. By default, it will display few items and when items exceed a certain limit, it will present vertical scroll bars. Java AWT List comes in flavors. They are:

  1. Single Select List
  2. Multi-Select List

In Single Select List, the user can select only one item. When they pick new one, AWT will deselect the past one. But, in Multi-Select List, one can select multiple items by holding the control key. Below is the screenshot of an AWT Multi-Select List:

Multi-Select AWT List
Multi-Select AWT List

2. The AWT List Control Example

Below is the screenshot of the sample AWT window, which we will create in this example:

Java AWT Multi-Select List Example
Java AWT Multi-Select List Example

In our example, we have a Multi-Select List box control in the upper left portion of the AWT Frame. When the user clicks the Get Fruits button, the AWT TextArea control will show all the selected list items in it. The Clear Output button will delete the text displayed in the AWT TextArea Control. This example will help you learn how to add strings on AWT List Control and how to retrieve all the items selected by the user. Now, we will proceed with the example.

3. Class Members

The FrameWindow class derived from AWT Frame has four class members. Here we declare, two button controls, one AWT TextArea and one List control. Also, this class implements ActionListener to handle the button click events of Get Fruits and Clear Output. Below is the code:

4. Adding Items to AWT List

While constructing the AWT List at line 2, we said how many items it should display without a scrollbar (param 1) and whether we need list box as a Multi-Select box or not (param 2). One can add items to the AWT List via add method call. In the below code snippet, we added few strings to our AWT Multi-Select List.

5. Prepare Other Controls

In Line 2-5, we create other supporting controls required for the example. After creating these controls, we added it to the AWT Frame Window using add method. Note, we register both the buttons with the ActionListener to receive the button click.

6. Get Selected Items Of AWT Multi-Select List

The method getSelectedItems will give all the selected items in AWT Multi-Selected List control. The return value is a String array. One can use getSelectedItem if the AWT List is set with a single select mode. Here, the return value is a String. Now have a look at the below code snippet which retrieves all the selected items from our AWT Multi-Select List component:

In the above code, the method getSource tells which component produced the ActionEvent. We use it to know which button user clicked. The call to getSelectedItems at line number 7 gives all the selected items as a string array and we iterate over it. In each iteration, we add the selected items to the AWT TextArea control by calling its append method. When the user clicked the clear button, we clear the TextArea by setting empty text to it.

7. Code Reference

7.1 MainEntryAWT.java

7.2 AWTMListExample

8. AWT Multi-Select List – Youtube Demo

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.