import javax.swing.*; public class Example1 { public static void main(String[] args) { JFrame frame = new JFrame("JList Example"); String[] items = {"Item 1", "Item 2", "Item 3"}; JListlist = new JList (items); frame.add(list); frame.pack(); frame.setVisible(true); } }
import javax.swing.*; import javax.swing.event.ListSelectionEvent; import javax.swing.event.ListSelectionListener; public class Example2 { public static void main(String[] args) { JFrame frame = new JFrame("JList Example"); String[] items = {"Item 1", "Item 2", "Item 3"}; JListBoth examples use the javax.swing package library to create and display the JList component.list = new JList (items); list.addListSelectionListener(new ListSelectionListener() { public void valueChanged(ListSelectionEvent e) { System.out.println(list.getSelectedValue()); } }); frame.add(list); frame.pack(); frame.setVisible(true); } }