//ListDemo.java //Demonstrates how to use the List component //Andy Harris, 05/00 import java.awt.*; import java.applet.*; import java.awt.event.*; public class ListDemo extends Applet implements ItemListener{ Label lblOutput = new Label("Sample Text"); List lstColor = new List(5); Panel pnlSouth = new Panel(); public void init(){ setLayout(new BorderLayout()); add(lblOutput, BorderLayout.CENTER); add(pnlSouth, BorderLayout.SOUTH); lblOutput.setAlignment(Label.CENTER); lblOutput.setFont(new Font("SansSerif", Font.PLAIN, 20)); pnlSouth.setLayout(new FlowLayout()); pnlSouth.add(lstColor); //set up color list lstColor.add("black"); lstColor.add("blue"); lstColor.add("cyan"); lstColor.add("gray"); lstColor.add("green"); lstColor.add("magenta"); lstColor.add("orange"); lstColor.add("pink"); lstColor.addItemListener(this); } // end init public void itemStateChanged(ItemEvent e){ String theColor = lstColor.getSelectedItem(); if (theColor.equals("black")){ lblOutput.setForeground(Color.black); } else if (theColor.equals("blue")){ lblOutput.setForeground(Color.blue); } else if (theColor.equals("cyan")){ lblOutput.setForeground(Color.cyan); } else if (theColor.equals("gray")){ lblOutput.setForeground(Color.gray); } else if (theColor.equals("green")){ lblOutput.setForeground(Color.green); } else if (theColor.equals("magenta")){ lblOutput.setForeground(Color.magenta); } else if (theColor.equals("orange")){ lblOutput.setForeground(Color.orange); } else if (theColor.equals("pink")){ lblOutput.setForeground(Color.pink); } // end if } // end itemStateChanged } // end class def