Java Swing – JList

In Swing, the basic list class is called JList. It supports the selection of one or more items from a list. Although the list often consists of strings, it is possible to create a list of just about any object that can be displayed. JList is so widely used in Java. JList provides several constructors:

JList(Object[] items)

It creates a JList that contains the items in the array specified by items.
JList is based on two models:

1. ListModel – It defines how access to the list data is achieved.
2. ListSelectionModel Interface – It defines methods that determine what list item or items are selected.

A JList generates a ListSelectionEvent when the user makes or changes a selection. This event is also generated when the user deselects an item. It is handled by implementing ListSelectionListener. This listener specifies only one method, that is called valueChanged().

void valueChanged(ListSelectionEvent le)

Here, le is a reference to the object that generated the event.

By default, a JList allows the user to select multiple ranges of items within the list, but you can change this behaviour by calling setSelectionMode( ), which is defined by JList.

void setSelectionMode(int mode)

Here, mode specifies the selection mode. It must be one of these values defined by ListSelectionModel:

1. SINGLE_SELECTION – You can obtain the index of the first item selected, which will also be the index of the only selected item when using the single-selection mode, by calling getSelectedIndex( ), shown here:

int getSelectedIndex()

Indexing begins at zero. So, if the first item is selected, this method will return 0. If no item is selected, –1 is returned.

2. SINGLE_INTERVAL_SELECTION – With the single-interval selection, the user can select one range of items. With the single selection, the user can select only a single item.

3. MULTIPLE_INTERVAL_SELECTION – The default, multiple-interval selection, lets the user select multiple ranges of items within a list.

Example:

import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
public class List extends JApplet
{
JList jlst;
JLabel jlab;
JScrollPane jscrlp;
String Cities[] = { "New York", "Chicago", "Houston", "Denver", "Los Angeles", "Kolkata",
"London", "Paris", "New Delhi",
"Hong Kong", "Tokyo", "Sydney" };
public void init()
{
try
{
SwingUtilities.invokeAndWait(new Runnable()
{
public void run()
{
makeGUI();
}
});
}
catch (Exception exc)
{
System.out.println("Can't create because of " + exc);
}
}
private void makeGUI()
{
setLayout(new FlowLayout());
jlst = new JList(Cities);
jlst.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
jscrlp = new JScrollPane(jlst);
jscrlp.setPreferredSize(new Dimension(120, 90));
jlab = new JLabel("Choose a City");
jlst.addListSelectionListener(new ListSelectionListener() {
public void valueChanged(ListSelectionEvent le)
{
int idx = jlst.getSelectedIndex();
if(idx != -1)
jlab.setText("Current selection: " + Cities[idx]);
else
jlab.setText("Choose a City");
}
});
add(jscrlp);
add(jlab);
}
}