import java.util.* ; import java.io.* ; import java.awt.* ; import java.awt.event.* ; import javax.swing.* ; import javax.swing.event.* ; class TerminationListener implements WindowListener { public void windowActivated(WindowEvent e) {} public void windowClosed(WindowEvent e) {} public void windowClosing(WindowEvent e) { System.exit(0) ; } public void windowDeactivated(WindowEvent e) {} public void windowDeiconified(WindowEvent e) {} public void windowIconified(WindowEvent e) {} public void windowOpened(WindowEvent e) {} } class ListPanel extends JPanel implements ListSelectionListener { public ListPanel() { String [] colors = {"black", "blue", "cyan", "darkGray", "gray", "green", "lightGray", "magenta", "orange", "pink", "red", "white", "yellow"} ; list = new JList(colors) ; list.setVisibleRowCount(6) ; // Default is 8 list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION) ; list.addListSelectionListener(this) ; add(new JScrollPane(list, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER)) ; } public void valueChanged(ListSelectionEvent evt) { if(!evt.getValueIsAdjusting()) { String color = (String) list.getSelectedValue() ; if(color.equals("black")) setBackground(Color.black) ; else if(color.equals("blue")) setBackground(Color.blue) ; else if(color.equals("cyan")) setBackground(Color.cyan) ; else if(color.equals("darkGray")) setBackground(Color.darkGray) ; else if(color.equals("gray")) setBackground(Color.gray) ; else if(color.equals("green")) setBackground(Color.green) ; else if(color.equals("lightGray")) setBackground(Color.lightGray) ; else if(color.equals("magenta")) setBackground(Color.magenta) ; else if(color.equals("orange")) setBackground(Color.orange) ; else if(color.equals("pink")) setBackground(Color.pink) ; else if(color.equals("red")) setBackground(Color.red) ; else if(color.equals("white")) setBackground(Color.white) ; else if(color.equals("yellow")) setBackground(Color.yellow) ; } } private JList list ; } public class ListTest { public static void main(String [] args) { JFrame frame = new JFrame() ; frame.setTitle("List") ; frame.setSize(450, 300) ; frame.addWindowListener(new TerminationListener()) ; frame.getContentPane().add(new ListPanel()) ; frame.setVisible(true) ; } }