import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.event.*; import javax.swing.border.*; public class List extends JFrame implements ListSelectionListener{ JList list; JPanel listPanel, outputPanel; JTextArea output; JScrollPane scrollPane; String [] listItems = { " Item 1 ", " Item 2 ", " Item 3 ", " Item 4 ", " Item 5 ", " Item 6 ", " Item 7 ", " Item 8 ", " Item 9 ", " Item 10 " }; public List( ){ //Make up for different sized monitors. Toolkit kit = getToolkit( ); Dimension size = kit.getScreenSize( ); setBounds( size.width/4, size.height/4, size.width/2, size.height/2); setDefaultCloseOperation( WindowConstants.DISPOSE_ON_CLOSE ); //click on X to shut down Container contentPane = getContentPane( ); contentPane.setLayout( new GridLayout( 1, 2 ) );//row, column listPanel = new JPanel( ); listPanel.setBackground( Color.yellow );//row outputPanel = new JPanel( ); outputPanel.setBackground( Color.white );//column contentPane.add( listPanel ); contentPane.add( outputPanel ); output = new JTextArea( 1, 10 ); output.setBorder( BorderFactory.createLineBorder( Color.blue ) ); //border could be different thicknesses. output.setEditable( false ); outputPanel.add( output ); list = new JList( listItems ); list.setSelectionBackground( Color.green ); //changes selected item list.setBorder( BorderFactory.createTitledBorder( "Items" ) ); //border around title. list.setSelectionMode( ListSelectionModel.SINGLE_SELECTION ); //can only choose one at a time list.addListSelectionListener( this ); //list.setEnabled( false ); scrollPane = new JScrollPane( list ); //listPanel.add( list ); listPanel.add( scrollPane ); list.setVisibleRowCount( 4 ); //4 items at a time. setVisible( true ); } public void valueChanged( ListSelectionEvent e ){ if( e.getSource( ) == list ){ int index = list.getSelectedIndex( ); output.setText( listItems[ index ] ); } } public static void main( String [] args ){ List list = new List( ); } }