import java.awt.Color; import java.awt.FlowLayout; import javax.swing.border.*; import javax.swing.JFrame; import javax.swing.JList; import javax.swing.JScrollPane; import javax.swing.event.ListSelectionEvent; import javax.swing.event.ListSelectionListener; public class ListFrame extends JFrame{ private static final long serialVersionUID = 1L; private JList colorList; private final String colorNames[]={"BLACK","BLUE","CYAN","ORANGE","DRAK_GRAY"}; private final Color colors[]={Color.black,Color.blue,Color.cyan,Color.orange,Color.DARK_GRAY}; private EtchedBorder eb= new EtchedBorder(1, Color.black, Color.gray); public ListFrame(){ super("List "); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setSize(300, 300); setLayout(new FlowLayout()); colorList = new JList(colorNames); colorList.setSelectionMode(0); colorList.setBorder(eb); add(new JScrollPane(colorList)); colorList.addListSelectionListener(new ListSelectionListener() { @Override public void valueChanged(ListSelectionEvent e) { getContentPane().setBackground(colors[colorList.getSelectedIndex()]); } }); add(colorList); setVisible(true); } public static void main(String args[]){ new ListFrame(); } }