JList Example in Swing for Beginners

The following example illustrates using JList in Swing. This example covers all core methods of javax.swing.JList class.

 import javax.swing.*;
import java.awt.*;
import java.util.*;
class JListExample extends JFrame
JList<String> l1,l2,l3,l4;
JScrollPane s1,s2,s3,s4;
final String data[]={"Apple","Apple","Mango","Orange","Banana"};
DefaultListModel<String> model=new DefaultListModel<>();
Vector<String> v=new Vector<>();
Dimension d=new Dimension(100,75);

    public JListExample()
    private void createAndShowGUI()
        setTitle("JList Example in Swing");
        setLayout(new FlowLayout());
        // A model contains data, it is a Vector implementation
        // Add items to model
        // Add items to vector
        // Create an empty list
        l1=new JList<>();
        // Create a list of String[] data
        // An array of 'any' type is supported
        l2=new JList<>(data);
        // Create a list with given model
        l3=new JList<>(model);
        // Create a list containing items in
        // the given vector
        l4=new JList<>(v);
        // Add some data to l1
        // You can also send 'v' (overloaded version)
        // Set some preferred size
        // Set horizontal wrap
        // Set selection mode
        // Multi select means the user can select multiple items
        // on ctrl+click
        // Get selection mode
        // Set selected index
        // Get selected index
        System.out.println("Selected index l2 is "+l2.getSelectedIndex());
        // Set selected item (case-sensitive)
        // Scroll to the value, to make it visible
        // here true and false is same, because of few items
        // first Apple will be selected
        // Get selected item
        System.out.println("Selected item in l1 is "+l1.getSelectedValue());
        // Select multiple indices
        l3.setSelectedIndices(new int[]{1,3});
        l2.setSelectedIndices(new int[]{3,1});
        // Set selection interval
        // select from 3rd index to 1st index
        // so that 1st index will be lead index
        // Add a selection interval
        // now lead index is updated from 1 to 4
        // Lead selection index is index of currently focused item
        System.out.println("Lead selection index for l2 "+l2.getLeadSelectionIndex());
        System.out.println("Lead selection index for l4 "+l4.getLeadSelectionIndex());
        // Now remove the selection interval
        // now 3 will become lead selection index
        System.out.println("Lead selection index for l4 "+l4.getLeadSelectionIndex());
        // Now clear selection
        System.out.println("Is l2 selection empty? "+l2.isSelectionEmpty());
        System.out.println("Lead selection index for l2 "+l2.getLeadSelectionIndex());
        // Get minimum,maximum selected index
        System.out.println("Minimum selected index for l3 is "+l3.getMinSelectionIndex());
        System.out.println("Maximum selected index for l3 is "+l3.getMaxSelectionIndex());
        // Returns -1 because selection is empty
        System.out.println("Minimum selected index for l2 is "+l2.getMinSelectionIndex());
        // Get first and last visible index
        System.out.println("First visible index is "+l4.getFirstVisibleIndex());
        System.out.println("Last visible index is "+l4.getLastVisibleIndex());
        // Get selected indices
        int indices[]=l3.getSelectedIndices();
            for(int k:indices) System.out.print(k+" ");
        // Get selected values
        // method getSelectedValues() is deprecated and replaced
        // by this method
        java.util.List<String> sval=l3.getSelectedValuesList();
            for(int i=0;i<sval.size();i++) System.out.print(sval.get(i)+" ");
        // Get item at given index
        System.out.println("\nItem at index 2 in l1 is "+l1.getModel().getElementAt(2));
        // Set a selection background,foreground
        // Create scroll panes for every JList
        s1=new JScrollPane(l1);
        s2=new JScrollPane(l2);
        s3=new JScrollPane(l3);
        s4=new JScrollPane(l4);
        // Add all items
    public static void main(String args[])
        new JListExample();
Also see adding and removing elements dynamically in JList

Posted by at on

Tags: Swing Components,