Advertisement



< Prev
Next >



JList class




JList class is used to create a list with multiple values, allowing a user to select any of these values. When a value is selected from JList, a ListSelectionEvent is generated, which is handled by implementing ListSelectionListener interface. JList is another lightweight component which extends JComponent class.





Constructors of JList


Constructor Description
public JList() Creates a JList with an empty, read-only, model.
public JList(E[] items) Creates a JList that is populated with the items of specfied array.
public JList(ListModel <E>) Creates a JList that displays elements from the specified ListModel.





Methods of JList class


Methods Description
public void addListSelectionListener(ListSelectionListener listener) Gets a String message of JList.
public void clearSelection() Appends the text to the JList.
public E getSelectedValue()) Gets the total number of rows in JList.





An example of JList


import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;

public class List1
{
public static void main(String... ar)
{
SwingUtilities.invokeLater(new Runnable() {
public void run()
{
new A();
}
});

}//Closing the main method
}//Closing the class Combo


class A 
{
String [] seasons;
JFrame jf;
JList<String> list;
JLabel label1;

A()
{
seasons= new String[]{"Spring", "Summer", "Monsoon", "Autumn", "Winter"};


 
jf= new JFrame("JList");
list= new JList<String>(seasons);
label1 = new JLabel("Select your favorite season from the list :");

jf.add(label1);
jf.add(list);

jf.setLayout(new FlowLayout());
jf.setSize(260,200);
jf.setVisible(true);
}

}

When you run the code, you are presented a window shown below -:

Figure 1



Advertisement




Creating and initializing a JList from an existing ListModel


In the upcoming code, we are going to handle events when a JRadioButton is checked or unchecked. In the next code, we have also combined a JRadioButton with a JLabel to add an icon/image next to a radio button.

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;

public class List2
{
public static void main(String... ar)
{
SwingUtilities.invokeLater(new Runnable() {
public void run()
{
new A();
}
});

}//Closing the main method
}//Closing the class Combo


class A //implements ActionListener
{
String [] continents;
JFrame jf;
DefaultListModel<String> list1;
JList<String> list2;
JLabel label1;


A()
{

list1= new DefaultListModel<String>();

list1.addElement("Asia");
list1.addElement("Africa");
list1.addElement("North America");
list1.addElement("South America");
list1.addElement("Antarctica");
list1.addElement("Europe");
list1.addElement("Australia");


list2= new JList<String>(list1);
jf= new JFrame("JList");

label1= new JLabel("Name of all the continents in the world -");

jf.add(label1);
jf.add(list2);

jf.setLayout(new FlowLayout());
jf.setSize(300,250);
jf.setVisible(true);
}

}
When you run the code, you are presented a window shown in the Figure2. This window displays a list containing all the continents in our world -:

Figure 2





Handling JList events


import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.event.ListSelectionListener;
import javax.swing.event.ListSelectionEvent;



public class list3
{
public static void main(String... ar)
{
SwingUtilities.invokeLater(new Runnable() {
public void run()
{
new A();
}
});

}//Closing the main method
}//Closing the class Combo4


class A implements ListSelectionListener
{
String [] fav;
JFrame jf;
JList<String> list;
JLabel label1, label2;

A()
{
fav = new String[]{"USA", "Switzerland", "India", "New Zealand", "Iceland"};


 
jf= new JFrame("JList");
list= new JList<String>();
label1 = new JLabel("Please select a country to visit");
label2 = new JLabel();

jf.add(label1);
jf.add(list);
jf.add(label2);


list.addListSelectionListener(this);

jf.setLayout(new FlowLayout());
jf.setSize(250,200);
jf.setVisible(true);
}



public void valueChanged(ListSelectionEvent e)
{
if (!e.getValueIsAdjusting()) 
{
label2.setText( (String)list.getSelectedValue() + " is selected from the list");
}
}

}
When you run the code, you are presented a window shown in the Figure3. In this window you are asked to select one of the countries in the list that you would like to visit -:

Figure 3


as soon as we clicked on the item Iceland, the window shows that we have selected the item Iceland from the list, shown in the figure below-

Figure 4




Please share this article -





< Prev
Next >
< JTable
JTree >



Advertisement

Please Subscribe

Please subscribe to our social media channels for daily updates.


Decodejava Facebook Page  DecodeJava Twitter Page Decodejava Google+ Page




Advertisement



Notifications



Please check our latest addition

C#, PYTHON and DJANGO


Advertisement