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.setBounds(50, 50, 100, 60);
        l2.setBounds(50, 200, 100, 60);

        f.setSize(300, 300);


    public static void main(String[] args) {
        CreateList ob = new CreateList();


Java AWT List

Java AWT List

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.



Comments and Discussions



Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© some rights reserved.