In Swing, lists are represented by the class JList
. Formally, JList
is a component that displays a list of objects and allows the user to select one or more items. As it’s one of the components, it inherits the JComponent
class.
A model (ListModel
class) maintains the contents of the list.
JList
The different constructors of JList
that can be used while creating a JList
object are as follows:
JList()
: This constructor is used to construct a JList
object with an empty, read-only model.
JList(E[] listData)
: This constructor is used to construct a JList
that displays the elements in the given array. This constructor creates a read-only model for the given array.
JList(ListModel<E> dataModel)
: This constructor is used to construct a JList
object that displays elements from the given list of models.
JList(Vector<? extends E> listData)
: This constructor is used to construct a JList
object that displays the elements in the given vector.
JList
classThe most commonly used methods of JList
class are as follows:
addListSelectionListener(ListSelectionListener listener)
: This method adds a listener to the specified JList
instance. The listener will be notified every time a change to the selection occurs. This is the preferred way of listening for selection state changes.
getModel()
: This method returns the data model that holds the list of objects displayed by the JList component.
getSelectedIndex()
: This method returns the smallest selected index:
It returns the current selected index when a single selection is made.
It returns the smallest selected index when multiple selections are made.
It returns -1
, when no selection is made.
getSelectedIndices()
: This method returns an array of all the selected indices in an increasing order.
getSelectedValue()
: This method returns the value of the smallest selected index:
null
, when no selection is made.getSelectedValuesList()
: This method returns a list of all the selected items in increasing order based on their indices in the list.
isSelectedIndex(index)
: This method checks if the given index is selected or not.
isSelectionEmpty()
: This method returns true
if no selection is made. Otherwise returns false
.
setListData(E[] listData)
: This method is used to create a read-only ListModel from an array of objects.
setModel(ListModel<E> model)
: This method sets the model that consists of the contents of the list. The action listeners are notified about the change in state. The method finally clears the lists selection.
setSelectedValue(Object anObject,boolean shouldScroll)
: This method is used to select the specified object from the list. The parameter shouldScroll
indicates whether to scroll to display the selected object.
Let’s look at the code below:
import javax.swing.*; import java.util.Vector; class Main{ static JList<String> createJList(){ Vector<String> itemsInList = new Vector<>(); itemsInList.add("educative"); itemsInList.add("edpresso"); itemsInList.add("educative-answers"); itemsInList.add("educative-courses"); JList<String> list = new JList<>(itemsInList); list.setBounds(100,100, 200,100); return list; } static JTextField createTextField(){ JTextField jTextField = new JTextField(); jTextField.setBounds(100, 220, 200, 25); return jTextField; } public static void main(String[] args) { JFrame jFrame = new JFrame(); JList<String> list = createJList(); JTextField jTextField = createTextField(); jFrame.add(list); jFrame.add(jTextField); jFrame.setSize(400,400); jFrame.setLayout(null); jFrame.setVisible(true); list.addListSelectionListener(e -> { String selectedText = list.getSelectedValue(); System.out.println("Selection changed to " + selectedText); jTextField.setText(selectedText); }); } }
createJList()
function is defined that creates a list with items and returns a list.createTextField()
function is defined that creates a text box and returns the created text box.JFrame
is createdFree Resources