Table of contents
1.
Introduction
2.
AWT List
3.
Class declaration
4.
AWT List Constructors
5.
AWT List Methods
6.
Java AWT List Example
6.1.
Example 1
6.2.
Example 2
7.
Frequently Asked Questions
7.1.
What is AWT?
7.2.
How many items can be selected from the list at a time?
7.3.
Which library package needs to import to use awt List?
8.
Conclusion
Last Updated: Mar 27, 2024
Easy

AWT List

Author SHIVAM SINHA
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

The List is a GUI component that displays a list of String values from which the user can select one or more values. The list can be configured as per the choice, and it can be configured as ‘single select’ or ‘multiple select.’ It inherits a component class.

Also Read About, Multithreading in java

AWT List

The AWT list is a component in AWT that extends the Component class. It is used to create a list with multiple values, and from those lists, a user can select any of the values. When the values are selected, then an ItemEvent gets generated, which is handled by implementing the ItemListener interface. Selecting and deselecting items from the list can be done at runtime if any class is implementing this interface. We will see these things through a simple example in a later section.

Class declaration

Following is the declaration for java. awt.List class:

public class List extends Component implements ItemSelectable, Accessible

 

Now, let’s see AWT List Class Constructors.

AWT List Constructors

AWT List Methods

To perform the operations on the list, we have so many methods. Some of the important and most used methods are:

You can also read about the Multiple Inheritance in Java.

Java AWT List Example

Example 1

We will create a list of components with 5 rows and add them to the frame in this example.

// importing awt class  
import java.awt.*;

public class ListExample1 {
   // class constructor
   ListExample1() {
      // creating the frame
      Frame f = new Frame();
      // creating the list of 5 rows
      List l1 = new List(5);

      // setting the position of list component
      l1.setBounds(100, 100, 75, 75);

      // adding list items into the list
      l1.add("Item 1");
      l1.add("Item 2");
      l1.add("Item 3");
      l1.add("Item 4");
      l1.add("Item 5");

      // adding the list to frame
      f.add(l1);

      // setting size, layout and visibility of frame
      f.setSize(400, 400);
      f.setLayout(null);
      f.setVisible(true);
   }

   // main method
   public static void main(String args[]) {
      new ListExample1();
   }
}
You can also try this code with Online Java Compiler
Run Code

OUTPUT :

 

Try and compile it on java online compiler.

In this example, we have created a frame, and then we have created a list of size 5. We have 5 dummy data, and after adding these data, we set the size, layout, and visibility of the frame.

Example 2

import java.awt.*;


public class CreateList {
    CreateList() {
        Frame frame = new Frame();
        List list1 = new List(5, true);
        List list2 = new List(2);


        list1.add("Apple");
        list1.add("Banana");
        list1.add("Mango");


        list2.add("C++");
        list2.add("Java");
        list2.add("Ruby");
        list2.add("Javascript");
        list2.add("Python");


        System.out.println(list1.getItem(1));
        System.out.println(list2.getItemCount());
        list2.remove(3);
        System.out.println(list2.getItemCount());


        list1.setBounds(50, 50, 100, 60);
        list2.setBounds(50, 200, 100, 60);


        frame.setLayout(null);
        frame.setVisible(true);
        frame.setSize(300, 300);


        frame.add(list1);
        frame.add(list2);
    }


    public static void main(String[] args) {
        CreateList obj = new CreateList();
    }
}
You can also try this code with Online Java Compiler
Run Code

OUTPUT :

We have created two lists objects, List1, and List2. List1 has an initial size of 5 rows, but we have added only 3 items to it. Similarly, List2 has an initial size of 2 rows, but we have added 5 items. 

In the object constructor, the first parameter specifies the row size, and the second parameter is a boolean value that specifies whether the user can select single or multiple values. If the second parameter is false, then the user can select only one item from the list, as in the case of list 2. If the parameter is true, then the user can select multiple items from the list, as in the case of list 1.

Know about Single Inheritance in Java in detail.

Frequently Asked Questions

What is AWT?

It stands for Abstract Window Toolkit. It is an API that Java programmers use to create GUI(Graphical User Interface) objects such as scroll bars, windows, and buttons.

How many items can be selected from the list at a time?

If the list is configured as ‘single select,’ you can select only one item from the list, and if it is configured as ‘multiple select,’ you can select multiple items from the list.

Which library package needs to import to use awt List?

We need to import class java. awt.List.

Conclusion

In this article, we have discussed the AWT List through different examples. We also discussed the constructors and methods of the AWT List.

Enhance your skills in Data Structures and AlgorithmsCompetitive ProgrammingJavaScriptSystem Design, and more with our Coding Ninjas Studio  Guided Path. If you want to sharpen your coding skills to the test, check out the mock test series and enter the contests on Coding Ninjas Studio! If you are just a beginner and want to know what questions big giants like Amazon, Microsoft, and Uber ask, check the problemsinterview experiences, and interview bundle for placement preparations.

You may consider our paid courses curated by experts to give your career an edge over others!

Do Upvote and Happy Learning!

Live masterclass