Java Swing Tutorial - Java Swing JList








A JList<T> is a Swing component that displays a list of choices and lets we select one or more choices.

The type parameter T is the type of elements it contains.

A JList can show multiple choices on the screen.

The following code creates a JList using an array

String[]  items = new String[]{"Spring", "Summer",  "Fall",  "Winter"}; 
JList<String> list = new JList<>(items);

The following code creates a JList using a Vector

Vector<String> items2 = new Vector<>(4); 
items2.add("Spring"); 
items2.add("Summer");
items2.add("Fall");
items2.add("Winter");
JList<String> list2 = new JList<>(items2);

To enable scroll on a JList, add it to a JScrollPane and add the JScrollPane to the container.

myContainer.add(new JScrollPane(myJList));

We can configure the orientation of a JList in three ways:

  • Vertical - the default, all items in a JList are displayed using one column and multiple rows.
  • Horizontal Wrapping - all items are arranged in a row and multiple columns. If not all items can fit into a row, new rows are added to display them as necessary.
  • Vertical Wrapping - all items are arranged in a column and multiple rows. If all items cannot fit into a column, new columns are added to display them as necessary.

We can use the setVisibleRowCount(int visibleRows) method to set the number of visible rows we would prefer to see in the list without a need to scroll.

We can set its layout orientation using its setLayoutOrientation(int orientation) method, where orientation value could be one of the three constants defined in the JList class:

  • JList.VERTICAL
  • JList.HORIZONTAL_WRAP
  • JList.VERTICAL_WRAP

We can configure the mode of selection for a JList using its setSelectionMode(int mode) method.

The mode value could be one of the following three values defined as constants in the ListSelectionModel interface.

  • SINGLE_SELECTION
  • SINGLE_INTERVAL_SELECTION
  • MUTIPLE_INTERVAL_SELECTION

In a single selection mode, we can only select one item at a time.

In a single interval selection mode, we can select multiple items. However, the items selected must always be contiguous. We can use the combination of Ctrl key or Shift key and the mouse to make contiguous selections.

In a multiple interval section, we can select multiple items without any restrictions.

To handle list selection event, we can add a list selection listener to a JList, which will notify us when a selection is changed.

The valueChanged() method of ListSelectionListener is called when a selection is changed. This method may also be called multiple times in the middle of one selection change.

We use the getValueIsAdjusting() method of the ListSelectionEvent object to make sure that selection changing is finalized.

myJList.addListSelectionListener((ListSelectionEvent e) ->  {
    // Make sure selection change  is final 
    if (!e.getValueIsAdjusting())  {
        
    }
});

To know the number of choices in a JList, call the getSize() method of its model.

int size = myJList.getModel().getSize();

The following table lists commonly Used Methods of the JList Class

IDMethod/Description
1void clearSelection()
Clears the selection made in the JList.
2void ensureIndexIsVisible(int index)
Makes sure the item at the specified index is visible.
3int getFirstVisibleIndex()
Returns the smallest visible index. If there is no visible item or list is empty, it returns -1.
4int getLastVisibleIndex()
Returns the largest visible index. If there is no visible item or list is empty, it returns -1.
5int getMaxSelectionIndex()
Returns the largest selected index. Returns -1 if there is no selection.
6int getMinSelectionIndex()
Returns the smallest selected index. Returns -1 if there is no selection.
7int getSelectedIndex()
Returns the smallest selected index. If JList selection mode is single selection, it returns the selected index. Returns -1 if there is no selection.
8int[] getSelectedIndices()
Returns the indices of all selected items in an int array. The array will have zero elements if there is no selection.
9E getSelectedValue()
Returns the first selected item. If the JList has single selection mode, it is the value of the selected item. Returns null if there is no selection in the JList.
10List getSelectedValuesList()
Returns a list of the selected items in increasing order based on their indices in the list. It there is no selected item, an empty list is returned.
11boolean isSelectedIndex(int index)
Returns true if the specified index is selected. Otherwise, it returns false.
12boolean isSelectionEmpty()
Returns true if there is no selection in the JList. Otherwise, it returns false.
13void setListData(E[] listData)
void setListData(Vector listData)
Sets the new list of choices in the JList.
14void setSelectedIndex(int index)
Selects an item at the specified index.
15void setSelectedIndices(int[] indices)
Selects items at the indices in specified array
16void setSelectedValue(Object item, boolean shouldScroll)
Selects the specified item if it exists in the list. Scrolls to the item to make it visible if the second argument is true.