import java.awt.*; import javax.swing.event.*; import javax.swing.*; import java.util.List; public class JListExample // creates a scrollable pane with a JList { public static void main(String[] args) { JFrame frame=new JFrame(""); frame.setSize(350,200); JLPanel p=new JLPanel(); frame.add(p); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); } public static class JLPanel extends JPanel implements ListSelectionListener // JList causes a ListSelectionEvent { private JLabel lab; // used to output the selected entries from the JList private JList list; // JList will just be a bunch of letters stored as Strings private String[] items={"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M"}; // the values for the JList public JLPanel() { lab=new JLabel(" "); // initialize the JLabel to be empty list=new JList(items); // create the JList consisting of the Strings in items JScrollPane jsp=new JScrollPane(list); // place the JList in a scrollable pane list.addListSelectionListener(this); // add a listener to the JList setLayout(new BorderLayout()); // put the JList and JLabel onto this JPanel add(jsp,BorderLayout.CENTER); add(lab,BorderLayout.EAST); } @SuppressWarnings("unchecked") // needed because getSelectedValuesList returns a List public void valueChanged(ListSelectionEvent e) // method invoked if any JList item is selected or deselected { List x=(List)list.getSelectedValuesList(); // get all of the selected items as a List String outList=" "; // this will be placed in the JLabel, start it off with a few blanks for(int i=0;i