How to work with lists in Swing
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.
Constructors of 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 aJListobject with an empty, read-only model. -
JList(E[] listData): This constructor is used to construct aJListthat 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 aJListobject that displays elements from the given list of models. -
JList(Vector<? extends E> listData): This constructor is used to construct aJListobject that displays the elements in the given vector.
Commonly used methods of JList class
The most commonly used methods of JList class are as follows:
-
addListSelectionListener(ListSelectionListener listener): This method adds a listener to the specifiedJListinstance. 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:- It returns the value of the current selected index when a single selection is made.
- It returns the value of the smallest selected index when multiple selections are made.
- It returns
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 returnstrueif no selection is made. Otherwise returnsfalse. -
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 parametershouldScrollindicates whether to scroll to display the selected object.
Code example
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);
});
}
}Code explanation
- Lines 1-2: Relevant classes are imported.
- Lines 6-15: The
createJList()function is defined that creates a list with items and returns a list. - Lines 17-21: The
createTextField()function is defined that creates a text box and returns the created text box. - Line 24: An instance of
JFrameis created - Line 27: The created list is added to the frame.
- Line 28: The created text box is added to the frame.
- Lines 32-36: An action listener is added to the list. As soon as an item in the list is clicked, the text in the text box gets updated with the clicked item.
Free Resources