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:
- Single Select List
- 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:

2. The AWT List Control Example
Below is the screenshot of the sample AWT window, which we will create in this 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:
1 2 3 4 5 6 7 8 9 |
//Sample 01: Get Action Listner Support public class FrameWindow extends Frame implements WindowListener, ActionListener { //Sample 02: Class Members Button btnGetFruits; Button btnClear; TextArea ta; List awtList; |
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.
1 2 3 4 5 6 7 8 9 10 11 12 |
//Sample 03: Create AWT List Control awtList = new List(5, true); awtList.add("Apple"); awtList.add("Orange"); awtList.add("Banana"); awtList.add("Grapes"); awtList.add("Pine Apple"); awtList.add("Strawberry"); awtList.add("Jack Fruit"); awtList.add("Papaya"); awtList.add("Water Melon"); awtList.add("Mango"); |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
//Sample 04: Create supporting Controls ta = new TextArea(5, 50); btnGetFruits = new Button("Get Fruits"); btnClear = new Button("Clear Output"); //Sample 05: Set the Layout and Add Controls setLayout(new FlowLayout()); add(awtList); add(btnGetFruits); add(btnClear); add(ta); //Sample 06: Register Button With Listener btnGetFruits.addActionListener(this); btnClear.addActionListener(this); |
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
@Override public void actionPerformed(ActionEvent e) { //Sample 07: Get the Selected Fruits From //Multi-Select AWT List if(e.getSource() == btnGetFruits) { String[] SelectedFruits = awtList.getSelectedItems(); if (SelectedFruits.length == 0) { ta.append("No Friut Selected\r\n" ); return; } for(String fruit: SelectedFruits) ta.append(fruit + "\r\n"); } //Sample 08: Clear Text Area if(e.getSource() == btnClear) ta.setText(""); } |
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
1 2 3 4 5 6 7 8 9 10 11 12 |
package AwtDemoPkg; import java.awt.Frame; public class MainEntryAwt { public static void main(String[] args) { //Sample 03: Make the FrameWindow Visible AWTMListExample fw = new AWTMListExample("JSplitPane Example"); fw.setVisible(true); } } |
7.2 AWTMListExample
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 |
package AwtDemoPkg; import java.awt.Button; import java.awt.FlowLayout; import java.awt.Frame; import java.awt.List; import java.awt.TextArea; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.WindowEvent; import java.awt.event.WindowListener; //Sample 01: Get Action Listner Support public class AWTMListExample extends Frame implements WindowListener, ActionListener { //Sample 02: Class Members Button btnGetFruits; Button btnClear; TextArea ta; List awtList; public AWTMListExample(String FrameTitle) { //Display the Frame Window super(FrameTitle); setSize(500, 250); setLocation(100,100); addWindowListener(this); //Sample 03: Create AWT List Control awtList = new List(5, true); awtList.add("Apple"); awtList.add("Orange"); awtList.add("Banana"); awtList.add("Grapes"); awtList.add("Pine Apple"); awtList.add("Strawberry"); awtList.add("Jack Fruit"); awtList.add("Papaya"); awtList.add("Water Melon"); awtList.add("Mango"); //Sample 04: Create supporting Controls ta = new TextArea(5, 50); btnGetFruits = new Button("Get Fruits"); btnClear = new Button("Clear Output"); //Sample 05: Set the Layout and Add Controls setLayout(new FlowLayout()); add(awtList); add(btnGetFruits); add(btnClear); add(ta); //Sample 06: Register Button With Listener btnGetFruits.addActionListener(this); btnClear.addActionListener(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: Get the Selected Fruits From //Multi-Select AWT List if(e.getSource() == btnGetFruits) { String[] SelectedFruits = awtList.getSelectedItems(); if (SelectedFruits.length == 0) { ta.append("No Friut Selected\r\n" ); return; } for(String fruit: SelectedFruits) ta.append(fruit + "\r\n"); } //Sample 08: Clear Text Area if(e.getSource() == btnClear) ta.setText(""); } } |
8. AWT Multi-Select List – Youtube Demo
Categories: AWT
Tags: add method, getSelectedItem, getSelectedItems, TextArea