by Dinesh Thakur Category: Swing

Lists are supported in Swings by the JList class. It is a very popular control as it allows the user to present a list of items in an easier and efficient manner. It also allows hiding the long list of items by making the lists scrollable. JList has been inherited from a long list of classes.

Table below shows the constructors of the JList class:

                     constructors of the JList

The simple example for creating a list using Swings that displays 15 items and reports which ones the user clicks is illustrated below.

import java.awt.*;
import javax.swing.*;
import javax.swing.event.*;
/*<APPLET code=list1.class WIDTH=300 HEIGHT=200> </APPLET>*/
public class list1 extends JApplet implements ListSelectionListener
    JList jlist;
    public void init()
          Container c= getContentPane();
          String[] str=new String[15];
          for(int i=0;i<15;i++)
                   str[i]="Item_Selection" +(i+1);
                   jlist = new JList(str);
                   JScrollPane scp= new JScrollPane(jlist);
                   c.setLayout(new FlowLayout());
             public void valueChanged(ListSelectionEvent lse)
                 String st="Choose Item";
The output for the same would look like:
                  List using Swing

About Dinesh Thakur

Dinesh ThakurDinesh Thakur holds an B.C.A, MCSE, MCDBA, CCNA, CCNP, A+, SCJP certifications. Dinesh authors the hugely popular blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps. For any type of query or something that you think is missing, please feel free to Contact us.

Related Articles