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:

  1. JList(): This constructor is used to construct a JList object with an empty, read-only model.

  2. 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.

  3. JList(ListModel<E> dataModel): This constructor is used to construct a JList object that displays elements from the given list of models.

  4. JList(Vector<? extends E> listData): This constructor is used to construct a JList object 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:

  1. 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.

  2. getModel(): This method returns the data model that holds the list of objects displayed by the JList component.

  3. 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.

  4. getSelectedIndices(): This method returns an array of all the selected indices in an increasing order.

  5. 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.
  6. getSelectedValuesList(): This method returns a list of all the selected items in increasing order based on their indices in the list.

  7. isSelectedIndex(index): This method checks if the given index is selected or not.

  8. isSelectionEmpty(): This method returns true if no selection is made. Otherwise returns false.

  9. setListData(E[] listData): This method is used to create a read-only ListModel from an array of objects.

  10. 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.

  11. 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.

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);
        });
    }
}
Lists in Swing

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 JFrame is 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

Copyright ©2025 Educative, Inc. All rights reserved