|
In this example, you will see how to create a List using JList component in Swing. The JList is used to create a list which contains items. This class extends the JComponent class in java swing. This Lists are used to select item from the group of item. JList is a component of GUI. The JList is same as JCombobox, but the little difference between JList and the JCombobox is that you can select only one item at a time in the combo box since you can select more than one item at a time in a list. In this example there are multiple java related technology items like: 'java', 'jsp', 'servlet', 'jdbc', and 'swing'. The complete code of example and output image is as follows:
Sample Code (SwingJList.java):
import java.awt.*;
import javax.swing.*;
public class SwingJList {
public static void main(String args[]) {
String l_item[] = {"Java", "JSP", "Servlet",
"JDBC","Swing"};
String h_title = "Swing JList Example";
JFrame frame = new JFrame(h_title);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JList list = new JList(l_item);
JScrollPane scrollPane = new JScrollPane(list);
Container contentPane = frame.getContentPane();
contentPane.add(scrollPane, BorderLayout.CENTER);
frame.setSize(200, 150);
frame.setVisible(true);
}
} |
|
|