Home »
Java programming language
Java AWT List
Java | AWT List: In this tutorial, we will look at one of the Java AWT components, the AWT List with example.
Submitted by Saranjay Kumar, on April 29, 2020
The List is a GUI component used to display a list of text items. It contains a set of String values that the user can choose from. It is a 'list' that allows the user to select one or more options. The programmer has the choice to configure 'single select' or 'multiple select' option of a list.
An object of List class generates an ItemEvent object when an item is selected from the list. This event can be handled by implementing the ItemListener interface. Any class implementing this interface can interact with the List object at runtime and handle selecting and unselecting of items from a list. We will study more about event handling in later sections.
Consider the following code -
import java.awt.*;
public class CreateList {
CreateList() {
Frame f = new Frame();
List l1 = new List(5, true);
List l2 = new List(2);
l1.add("Apple");
l1.add("Banana");
l1.add("Mango");
l2.add("C++");
l2.add("Java");
l2.add("Ruby");
l2.add("Javascript");
l2.add("Python");
System.out.println(l1.getItem(1));
System.out.println(l2.getItemCount());
l2.remove(3);
System.out.println(l2.getItemCount());
l1.setBounds(50, 50, 100, 60);
l2.setBounds(50, 200, 100, 60);
f.setLayout(null);
f.setVisible(true);
f.setSize(300, 300);
f.add(l1);
f.add(l2);
}
public static void main(String[] args) {
CreateList ob = new CreateList();
}
}
Output
We created two List objects l1 and l2. L1 has been given an initial size of 5 rows but has been populated with only 3 items. L2 has been given an initial size of 2 rows but has been populated with 5 items. The first parameter of the object constructor specifies the row size. The second parameter is a boolean value which specifies whether multiple options can be chosen by the user. The default value is false, meaning that the user can choose only one item from the list, as is the case with l2. If we set this parameter as true, the user will have the freedom to choose multiple items from the list, as can be seen in the case of l1.
We can add various text items to a list object using the add() method. A list can hold String values that are passed as a parameter during the call to the add() method. Items are displayed in the same order in which they are added to the list. Items have an index attached to them, using which we can access them directly.
To access an item at a specified index, we use the getItem() method, in which we pass the index of the item to fetch. The getItem() returns a String value, of the item at that particular index.
We can also retrieve the number of items added in a list by using the getItemCount() method. As can be seen, it returns an integer value, specifying the number of elements present in the list.
If we want to remove a particular item from the list, we can use the remove() method. We can pass either the String value that is to be removed or the index of the element to be removed.
As can be seen from the output of the code, initially we get 5 items present in l2. However, after using the remove() method, we get 4 elements remaining in the list.