Java JList Basics with Examples

JList is used to display an array of vectors in Java. It comes with Swing package and also it inherits JComponent Class. JList displays a set of elements and allows the user to make the selection for one or more items. You can also check selected elements of the list using different methods given below.

JList Constructors

Following are the constructors for JList in Java:

  • JList(): Used to create empty blank list.
  • JList(arry[] listData): Used to create new List with elements provided by the array.
  • JList(ListModel<arry> dataModel): Creates new List with provided list Model.

Commonly Used Methods

Following is the list of commonly used methods:

getSelectedValue()It will return the value of element selected by user.
getSelectedIndex()It will return the index of element selected by user.
setSelectedIndex(int i)Sets value of i as selected index.
setListData(E [ ] le)Sets the elements of le replacing existing.
setSelectionBackground(Color c)It sets the background color for selected item.
setSelectionForeground(Color c)Sets the foreground color for selected item.
setVisibleRowCount(int v)Sets the visibleRowCount property to v
setSelectedValue(Object a, boolean s)It sets the value to selected element
setSelectedIndices(int[] i)Sets all the items from given indices array as selected
setListData(Vector l)constructs a read only list model with provided vector
setLayoutOrientation(int l)sets orientation of layout
setFixedCellWidth(int width)Changes the cell width
setFixedCellHeight(int height)Changes the cell height
isSelectedIndex(int i)Returns true if index provided is selected
indexToLocation(int i)It returns the origin of given item
getToolTipText(MouseEvent e)Returns the tooltip text for given event
getSelectedValuesList()Returns the list of selected item values
getSelectedIndices()It returns the list of selected indices in incresing order
getMinSelectionIndex()returns the smallest index of selected value
getMaxSelectionIndex()returns the biggest index of selected value
getListSelectionListeners()It returns the listeners of list
getLastVisibleIndex()returns the index of last visible element
getDragEnabled()returns true if drag enabled
addListSelectionListener(listSelectionListener l)adds listSelectionListener l to list

Add Item to JList

To add an element, you should add it first to the ListModel then set the ListModel to JList.

Remove Item from JList

To remove an element, You should remove it from the ListModel:

Set Listener

You can add a listener in the following way:


Following are the examples for JList. The first example is the simple example and in the second example on more element Button is added.

Example 1’st

Below is the output for the above program which shows the default screen of the program in which the list item April is selected by default:


Example 2’nd

Below is the output for the above program. The first screenshot shows the default screen after running program And the second screen shows output after clicking on SHOW button:




Please enter your comment!
Please enter your name here