Open In App

Java Swing | JList with examples

Improve
Improve
Like Article
Like
Save
Share
Report

JList is part of Java Swing package . JList is a component that displays a set of Objects and allows the user to select one or more items . JList inherits JComponent class. JList is a easy way to display an array of Vectors .
Constructor for JList are : 
 

  1. JList(): creates an empty blank list
  2. JList(E [ ] l) : creates an new list with the elements of the array.
  3. JList(ListModel d): creates a new list with the specified List Model
  4. JList(Vector l) : creates a new list with the elements of the vector

Commonly used methods are : 

 

method explanation
getSelectedIndex() returns the index of selected item of the list
getSelectedValue() returns the selected value of the element of the list
setSelectedIndex(int i) sets the selected index of the list to i
setSelectionBackground(Color c) sets the background Color of the list
setSelectionForeground(Color c) Changes the foreground color of the list
setListData(E [ ] l) Changes the elements of the list to the elements of l .
setVisibleRowCount(int v) Changes the visibleRowCount property
setSelectedValue(Object a, boolean s) selects the specified object from the list.
setSelectedIndices(int[] i) changes the selection to be the set of indices specified by the given array.
setListData(Vector l) constructs a read-only ListModel from a Vector specified.
setLayoutOrientation(int l) defines the orientation of the list
setFixedCellWidth(int w) Changes the cell width of list to the value passed as parameter.
setFixedCellHeight(int h) Changes the cell height of the list to the value passed as parameter.
isSelectedIndex(int i) returns true if the specified index is selected, else false.
indexToLocation(int i) returns the origin of the specified item in the list’s coordinate system.
getToolTipText(MouseEvent e) returns the tooltip text to be used for the given event.
getSelectedValuesList() returns a list of all the selected items.
getSelectedIndices() returns an array of all of the selected indices, in increasing order
getMinSelectionIndex() returns the smallest selected cell index, or -1 if the selection is empty.
getMaxSelectionIndex() returns the largest selected cell index, or -1 if the selection is empty.
getListSelectionListeners() returns the listeners of list
getLastVisibleIndex() returns the largest list index that is currently visible.
getDragEnabled() returns whether or not automatic drag handling is enable
addListSelectionListener(ListSelectionListener l) adds a listSelectionlistener to the list

The Following programs will illustrate the use of JLists 
1. Program to create a simple JList
 

Java




// java Program to create a simple JList
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
class solve extends JFrame
{
     
    //frame
    static JFrame f;
     
    //lists
    static JList b;
  
 
    //main class
    public static void main(String[] args)
    {
        //create a new frame
        f = new JFrame("frame");
         
        //create a object
        solve s=new solve();
       
        //create a panel
        JPanel p =new JPanel();
         
        //create a new label
        JLabel l= new JLabel("select the day of the week");
 
        //String array to store weekdays
        String week[]= { "Monday","Tuesday","Wednesday",
                         "Thursday","Friday","Saturday","Sunday"};
         
        //create list
        b= new JList(week);
         
        //set a selected index
        b.setSelectedIndex(2);
         
        //add list to panel
        p.add(b);
  
        f.add(p);
         
        //set the size of frame
        f.setSize(400,400);
          
        f.show();
    }
     
     
}


Output : 
 

2. Program to create a list and add itemListener to it (program to select your birthday using lists) . 
 

Java




// java Program to create a list and add itemListener to it
// (program to select your birthday using lists) .
import javax.swing.event.*;
import java.awt.*;
import javax.swing.*;
class solve extends JFrame implements ListSelectionListener
{
     
    //frame
    static JFrame f;
     
    //lists
    static JList b,b1,b2;
     
    //label
    static JLabel l1;
  
 
    //main class
    public static void main(String[] args)
    {
        //create a new frame
        f = new JFrame("frame");
         
        //create a object
        solve s=new solve();
       
        //create a panel
        JPanel p =new JPanel();
         
        //create a new label
        JLabel l= new JLabel("select your birthday");
        l1= new JLabel();
 
        //String array to store weekdays
        String month[]= { "January", "February", "March",
        "April", "May", "June", "July", "August",
        "September", "October", "November", "December"};
         
        //create a array for months and year
        String date[]=new String[31],year[]=new String[31];
         
        //add month number and year to list
        for(int i=0;i<31;i++)
        {
            date[i]=""+(int)(i+1);
            year[i]=""+(int)(2018-i);
        }
         
        //create lists
        b= new JList(date);
        b1= new JList(month);
        b2= new JList(year);
         
        //set a selected index
        b.setSelectedIndex(2);
        b1.setSelectedIndex(1);
        b2.setSelectedIndex(2);
         
        l1.setText(b.getSelectedValue()+" "+b1.getSelectedValue()
                              +" "+b2.getSelectedValue());
         
        //add item listener
        b.addListSelectionListener(s);
        b1.addListSelectionListener(s);
        b2.addListSelectionListener(s);
         
        //add list to panel
        p.add(l);
        p.add(b);
        p.add(b1);
        p.add(b2);
        p.add(l1);
  
        f.add(p);
         
        //set the size of frame
        f.setSize(500,600);
          
        f.show();
    }
    public void valueChanged(ListSelectionEvent e)
    {
        //set the text of the label to the selected value of lists
        l1.setText(b.getSelectedValue()+" "+b1.getSelectedValue()
                              +" "+b2.getSelectedValue());
         
    }
     
     
}


Output : 
 

Note : The above programs might not run in an Online compiler please use an Offline IDE
 



Last Updated : 16 Apr, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads