Java Swing | JList with examples – GeeksforGeeks

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 : 

 

methodexplanationgetSelectedIndex()returns the index of selected item of the listgetSelectedValue()returns the selected value of the element of the listsetSelectedIndex(int i)sets the selected index of the list to isetSelectionBackground(Color c)sets the background Color of the listsetSelectionForeground(Color c)Changes the foreground color of the listsetListData(E [ ] l)Changes the elements of the list to the elements of l .setVisibleRowCount(int v)Changes the visibleRowCount propertysetSelectedValue(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 listsetFixedCellWidth(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 ordergetMinSelectionIndex()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 listgetLastVisibleIndex()returns the largest list index that is currently visible.getDragEnabled()returns whether or not automatic drag handling is enableaddListSelectionListener(ListSelectionListener l)adds a listSelectionlistener to the list

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

Tóm Tắt

Java




import java.awt.event.*;

import java.awt.*;

import javax.swing.*;

class solve extends JFrame

{

     

    

    static JFrame f;

     

    

    static JList b;

  

 

    

    public static void main(String[] args)

    {

        

        f = new JFrame("frame");

         

        

        solve s=new solve();

       

        

        JPanel p =new JPanel();

         

        

        JLabel l= new JLabel("select the day of the week");

 

        

        String week[]= { "Monday","Tuesday","Wednesday",

                         "Thursday","Friday","Saturday","Sunday"};

         

        

        b= new JList(week);

         

        

        b.setSelectedIndex(2);

         

        

        p.add(b);

  

        f.add(p);

         

        

        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




import javax.swing.event.*;

import java.awt.*;

import javax.swing.*;

class solve extends JFrame implements ListSelectionListener

{

     

    

    static JFrame f;

     

    

    static JList b,b1,b2;

     

    

    static JLabel l1;

  

 

    

    public static void main(String[] args)

    {

        

        f = new JFrame("frame");

         

        

        solve s=new solve();

       

        

        JPanel p =new JPanel();

         

        

        JLabel l= new JLabel("select your birthday");

        l1= new JLabel();

 

        

        String month[]= { "January", "February", "March",

        "April", "May", "June", "July", "August",

        "September", "October", "November", "December"};

         

        

        String date[]=new String[31],year[]=new String[31];

         

        

        for(int i=0;i<31;i++)

        {

            date[i]=""+(int)(i+1);

            year[i]=""+(int)(2018-i);

        }

         

        

        b= new JList(date);

        b1= new JList(month);

        b2= new JList(year);

         

        

        b.setSelectedIndex(2);

        b1.setSelectedIndex(1);

        b2.setSelectedIndex(2);

         

        l1.setText(b.getSelectedValue()+" "+b1.getSelectedValue()

                              +" "+b2.getSelectedValue());

         

        

        b.addListSelectionListener(s);

        b1.addListSelectionListener(s);

        b2.addListSelectionListener(s);

         

        

        p.add(l);

        p.add(b);

        p.add(b1);

        p.add(b2);

        p.add(l1);

  

        f.add(p);

         

        

        f.setSize(500,600);

          

        f.show();

    }

    public void valueChanged(ListSelectionEvent e)

    {

        

        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
 

My Personal Notes

arrow_drop_up