Implementation of List Selection Listener in Java

Introduction

This article explains how to implement a List Selection Listener in Java. The Netbeans IDE is used for the development of the program.

What is List Selection Listener

The List Selection Listener is basically invoked when the selection in the list has changed. These events are generally fired from the objects that implement the ListSelectionModel interface.

Methods inside List Selection Listener

  • valueChaanged(ListSelectionEvent)

          This method is called in response to selection changes.

  • Object getSource()

          Returns the object that fires the event.

  • getFirstIndex()

          This method gets the index of the first item.

  • getLastIndex()

          This method gets the index of the last item.

Packages Imported

javax.swing.*;

Swing is an extended version of the AWT and provides a Graphical User Interface for programs. Swing is used to add Swing components, like Jlabel, JTextArea so on.

javax.swing.event.*;

This package is imported to handle the events fired by Swing components.

Example

In this example, we implement a List Selection Listener in Java using the Netbeans IDE. There are certain steps in the Netbeans IDE that we need to follow as explained below.

Step 1

Open the Netbeans IDE.

l1.jpg

Step 2

Click on "File" from the Menu bar as in the following:

l2.jpg

Step 3

Click on "New Project" as in the following:

l3.jpg

Step 4

Select "Java" and "Java application" then click on "Next" as in the following:

l4.jpg

Step 5

Instead of the project name specify "ListListenerDemo" and instead of the main class also specify "ListListenerDemo" and click on "Finish".

l5.jpg

Step 6

In this class write the following code (the class name is "ListListenerDemo"):

import javax.swing.*;

import javax.swing.event.*;

public class ListListenerDemo

{

 public static void main(final String args[])

 {

 final String labels[] = { "Vishal", "Sumit", "Devendra", "Dany", "Erick" };

 JFrame f = new JFrame("Selecting JList");

 f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

 JList l = new JList(labels);

 JScrollPane scrollPane1 = new JScrollPane(l);

 f.add(scrollPane1);

 ListSelectionListener ltd = new ListSelectionListener()

 {

  public void valueChanged(ListSelectionEvent listSelectionEvent)

  {

   System.out.println("First index: " + listSelectionEvent.getFirstIndex());

   System.out.println(", Last index: " + listSelectionEvent.getLastIndex());

   boolean adjust = listSelectionEvent.getValueIsAdjusting();

   System.out.println(", Adjusting? " + adjust);

   if (!adjust)

   {

    JList ls = (JList) listSelectionEvent.getSource();

    int selections[] = ls.getSelectedIndices();

    Object selectedValues[] = ls.getSelectedValues();

    for (int i = 0, n = selections.length; i < n; i++)

    {

     if (i == 0)

     {

      System.out.println("  Selections: ");

     }

      System.out.println(selections[i] + "/" + selectedValues[i] + " ");

    }

   }

  }

 };

 l.addListSelectionListener(ltd);

 f.setSize(350, 200);

 f.setVisible(true);

 }

}

           

l6.jpg

l6a.jpg

Step 7

Now go to "ListListenerDemo.java" and right-click on that, click on "Run" from the menu bar as in the following:

 

l7.jpg

 

Output

 

In the following window a list of names will be as in the following:

 

l8.jpg

 

Now when we select a name the List Selection Listener event is fired as in the following:

 

l9.jpg


Up Next
    Ebook Download
    View all
    Learn
    View all