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:
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:
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.
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
ID | Method/Description |
---|---|
1 | void clearSelection() Clears the selection made in the JList. |
2 | void ensureIndexIsVisible(int index) Makes sure the item at the specified index is visible. |
3 | int getFirstVisibleIndex() Returns the smallest visible index. If there is no visible item or list is empty, it returns -1. |
4 | int getLastVisibleIndex() Returns the largest visible index. If there is no visible item or list is empty, it returns -1. |
5 | int getMaxSelectionIndex() Returns the largest selected index. Returns -1 if there is no selection. |
6 | int getMinSelectionIndex() Returns the smallest selected index. Returns -1 if there is no selection. |
7 | int 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. |
8 | int[] getSelectedIndices() Returns the indices of all selected items in an int array. The array will have zero elements if there is no selection. |
9 | E 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. |
10 | List 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. |
11 | boolean isSelectedIndex(int index) Returns true if the specified index is selected. Otherwise, it returns false. |
12 | boolean isSelectionEmpty() Returns true if there is no selection in the JList. Otherwise, it returns false. |
13 | void setListData(E[] listData) void setListData(Vector listData) Sets the new list of choices in the JList. |
14 | void setSelectedIndex(int index) Selects an item at the specified index. |
15 | void setSelectedIndices(int[] indices) Selects items at the indices in specified array |
16 | void 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. |