Java JList

Education is not limited to just classrooms. It can be gained anytime, anywhere... - Ravi Ranjan (M.Tech-NIT)

Java JList

The object of JList class represents a list of text items. The list of text items can be set up so that the user can choose either one item or multiple items. It inherits JComponent class.

JList class declaration

Let's see the declaration for javax.swing.JList class.

  1. public class JList extends JComponent implements Scrollable, Accessible  

Commonly used Constructors:

Constructor

Description

JList()

Creates a JList with an empty, read-only, model.

JList(ary[] listData)

Creates a JList that displays the elements in the specified array.

JList(ListModel dataModel)

Creates a JList that displays elements from the specified, non-null, model.

Commonly used Methods:

Methods

Description

Void addListSelectionListener(ListSelectionListener listener)

It is used to add a listener to the list, to be notified each time a change to the selection occurs.

int getSelectedIndex()

It is used to return the smallest selected cell index.

ListModel getModel()

It is used to return the data model that holds a list of items displayed by the JList component.

void setListData(Object[] listData)

It is used to create a read-only ListModel from an array of objects.

Java JList Example

  1. import javax.swing.*;  
  2. public class ListExample  
  3. {  
  4.      ListExample(){  
  5.         JFrame f= new JFrame();  
  6.         DefaultListModel<String> l1 = new DefaultListModel<>();  
  7.           l1.addElement("Item1");  
  8.           l1.addElement("Item2");  
  9.           l1.addElement("Item3");  
  10.           l1.addElement("Item4");  
  11.           JList<String> list = new JList<>(l1);  
  12.           list.setBounds(100,10075,75);  
  13.           f.add(list);  
  14.           f.setSize(400,400);  
  15.           f.setLayout(null);  
  16.           f.setVisible(true);  
  17.      }  
  18. public static void main(String args[])  
  19.     {  
  20.    new ListExample();  
  21.     }}  

Output

JAVA Jlist 1

Java JList Example with ActionListener

  1. import javax.swing.*;  
  2. import java.awt.event.*;  
  3. public class ListExample  
  4. {  
  5.      ListExample(){  
  6.         JFrame f= new JFrame();  
  7.         final JLabel label = new JLabel();          
  8.         label.setSize(500,100);  
  9.         JButton b=new JButton("Show");  
  10.         b.setBounds(200,150,80,30);  
  11.         final DefaultListModel<String> l1 = new DefaultListModel<>();  
  12.           l1.addElement("C");  
  13.           l1.addElement("C++");  
  14.           l1.addElement("Java");  
  15.           l1.addElement("PHP");  
  16.           final JList<String> list1 = new JList<>(l1);  
  17.           list1.setBounds(100,10075,75);  
  18.           DefaultListModel<String> l2 = new DefaultListModel<>();  
  19.           l2.addElement("Turbo C++");  
  20.           l2.addElement("Struts");  
  21.           l2.addElement("Spring");  
  22.           l2.addElement("YII");  
  23.           final JList<String> list2 = new JList<>(l2);  
  24.           list2.setBounds(100,20075,75);  
  25.           f.add(list1); f.add(list2); f.add(b); f.add(label);  
  26.           f.setSize(450,450);  
  27.           f.setLayout(null);  
  28.           f.setVisible(true);  
  29.           b.addActionListener(new ActionListener() {  
  30.               public void actionPerformed(ActionEvent e) {   
  31.                  String data = "";  
  32.                  if (list1.getSelectedIndex() != -1) {                       
  33.                     data = "Programming language Selected: " + list1.getSelectedValue();   
  34.                     label.setText(data);  
  35.                  }  
  36.                  if(list2.getSelectedIndex() != -1){  
  37.                     data += ", FrameWork Selected: ";  
  38.                     for(Object frame :list2.getSelectedValues()){  
  39.                        data += frame + " ";  
  40.                     }  
  41.                  }  
  42.                  label.setText(data);  
  43.               }  
  44.            });   
  45.      }  
  46. public static void main(String args[])  
  47.     {  
  48.    new ListExample();  
  49.     }}  

Output:

JAVA Jlist 2