List Class and JList Class

List Class

The List component presents the user with scrolling list of text items. This class is used to create a pop-up list of item from which user may choose.

Declaration of List Class

public class List
      extends Component
           implements ItemSelectable, Accessible

List Class Constructors

ConstructorDescription
List()Used to create new scrolling list.
List(int rows)Creates a new scrolling list initialized with specified number of visible lines.
List(int rows, boolean multipleMode)Constructs a new scrolling list initialized to display the specified number of rows.

Example : Program to demonstrate choice list

//ListDemo.java

import java.awt.*;
import java.awt.event.*;
class ListDemo extends Frame implements ItemListener
{
      Choice country;
      public ListDemo(String title)
      {
            super(title);
            setLayout(new FlowLayout());
            country = new Choice();
            country.add("INDIA");
            country.add("CHINA");
            country.add("JAPAN");
            country.add("NEPAL");
            country.add("ENGLAND");
            country.add("KENYA");
            country.add("AMERICA");
            add(country);
            country.addItemListener(this);
      }
      public void itemStateChanged(ItemEvent e)
      {
      
      }
      public static void main(String args[])
      {
            ListDemo frame = new ListDemo("AWT ListDemo ");
            frame.setSize(200,200);
            frame.setBackground(Color.pink);
            frame.setVisible(true);
      }
}


Output:

list class

JList Class


The JList is a Swing component that can be used to display a list of elements. It provides more control than AWT controls. JList allows the user to select one or more items.

Declaration of JList Class

public class JList
      extends JComponent
           implements Scrollable, Accessible

JList Class Constructors

ConstructorDescription
JList()Constructs a JList with an empty, read-only, model.
JList(E[] ListData)Constructor creates a JList that’s displays element in the specified arrays.
JList(ListModel<E> dataModel)Constructs a JList that display elements from the specified, non-null model.
JList(Vector <? extends E> listDate)Used to construct a JList that displays the elements in the specified Vector.