// Purpose. JList demo import javax.swing.*; import java.awt.event.*; import javax.swing.event.*; // ListSelectionListener public class JListDemo { public static JPanel createList( String[] choices ) { JPanel lPanel = new JPanel(); JList list = new JList( choices ); list.setVisibleRowCount( 4 ); JScrollPane lsp = new JScrollPane( list ); lPanel.add( lsp ); ListSelectionListener lsl = new ListSelectionListener() { public void valueChanged( ListSelectionEvent e ) { if (e.getValueIsAdjusting() == false) return; System.out.println( ((JList)e.getSource()).getSelectedValue() ); } }; list.addListSelectionListener( lsl ); return lPanel; } public static void main( String[] args ) { JFrame frame = new JFrame( "JList demo" ); frame.addWindowListener( new WindowAdapter() { public void windowClosing( WindowEvent e ) { System.exit( 0 ); } } ); String[] choices = { "Alpha","Bravo","Charlie","Delta","Echo","Foxtrot" }; frame.getContentPane().add( createList( choices ) ); frame.setSize( 300, 240 ); frame.setVisible( true ); } } // Echo // Charlie // Delta // Foxtrot