Java Swing Tutorials

Create a JList Component
previous Home next

In this page of the tutorials, we are going to learn how to create a JList component of swing in java. JList is a component of GUI(graphical user interface). It is allow to show the multiple items in a list and some times it shows the data in multiple columns in a list.

The Lists control is used to select item from the item's group like combo box. but the JList and Combo box has major difference between us that we can select only one item from the combo box but JList box allow to select more than one item at once from the list. You can easily add or remove items from the JList component.

Method Description

JList: This is the class of the swing which is used to create a list which contain more than one items. This class extends the JComponent class in java swing.

 

Example

package r4r;
import javax.swing.*;
public class JListtest {
public static void main(String[] args) {
String sub[] = {"Math", "Biology", "Phisics", "Chemestry"};
JFrame frame = new JFrame("Creating a JList Components");
JPanel panel = new JPanel();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JList lst = new JList(sub);
frame.setUndecorated(true);
frame.getRootPane().setWindowDecorationStyle(JRootPane.PLAIN_DIALOG);
panel.add(lst);
frame.add(panel);
frame.setSize(500,500);
frame.setVisible(true);
}
}
previous Home next