//FontChooser.java //Illustrates Item Components //Andy Harris, 05/00 import java.awt.*; import java.applet.*; import java.awt.event.*; public class FontChooser extends Applet implements ItemListener{ //Control panel Panel pnlSouth = new Panel(); Label lblOutput = new Label("Sample text"); //size stuff Panel pnlSize = new Panel(); CheckboxGroup cbgSize = new CheckboxGroup(); Checkbox cb10 = new Checkbox("10", cbgSize, true); Checkbox cb20 = new Checkbox("20", cbgSize, false); Checkbox cb30 = new Checkbox("30", cbgSize, false); Checkbox cb40 = new Checkbox("40", cbgSize, false); //Color stuff List lstColor = new List(5); //Font stuff Panel pnlFontStyle = new Panel(); Choice chcFont = new Choice(); Checkbox chkBold = new Checkbox("bold"); Checkbox chkItalic = new Checkbox("italic"); public void init(){ //main layout setLayout(new BorderLayout()); add(lblOutput, BorderLayout.CENTER); add(pnlSouth, BorderLayout.SOUTH); lblOutput.setAlignment(Label.CENTER); lblOutput.setFont(new Font("SansSerif", Font.PLAIN, 10)); pnlSouth.setLayout(new GridLayout(1,0)); pnlSouth.add (pnlSize); pnlSouth.add (lstColor); pnlSouth.add (pnlFontStyle); //set up font size control pnlSize.setLayout(new GridLayout(0,1)); pnlSize.add(cb10); pnlSize.add(cb20); pnlSize.add(cb30); pnlSize.add(cb40); //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"); //set up font choicebox chcFont.add("SansSerif"); chcFont.add("Serif"); chcFont.add("Monospaced"); //set up panel for font and style pnlFontStyle.setLayout(new GridLayout(0,1)); pnlFontStyle.add(chcFont); pnlFontStyle.add(chkBold); pnlFontStyle.add(chkItalic); //add all the listeners cb10.addItemListener(this); cb20.addItemListener(this); cb30.addItemListener(this); cb40.addItemListener(this); lstColor.addItemListener(this); chcFont.addItemListener(this); chkBold.addItemListener(this); chkItalic.addItemListener(this); } // end init public void itemStateChanged(ItemEvent e){ //create variables for font Font oldFont = lblOutput.getFont(); Font newFont = lblOutput.getFont(); String fontName = oldFont.getName(); int fontStyle = oldFont.getStyle(); int fontSize = oldFont.getSize(); //System.out.println(e.getItemSelectable()); if (e.getItemSelectable() == lstColor){ 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 } else if (e.getItemSelectable() == chcFont){ //The user wants to change the font face fontName = chcFont.getSelectedItem(); } else if (e.getItemSelectable() == cb10 || e.getItemSelectable() == cb20 || e.getItemSelectable() == cb30 || e.getItemSelectable() == cb40 ){ //handle size comparison String fontString = String.valueOf(e.getItem()); //convert to int fontSize = Integer.parseInt(fontString); } else if (e.getItemSelectable() == chkBold || e.getItemSelectable() == chkItalic){ fontStyle = Font.PLAIN; if (chkBold.getState() == true){ fontStyle += Font.BOLD; } // end 'bold' if if (chkItalic.getState() == true){ fontStyle += Font.ITALIC; } // end 'italic' if } // end "what item clicked' if //apply the new details newFont = new Font(fontName, fontStyle, fontSize); System.out.println(newFont); lblOutput.setFont(newFont); } // end itemStateChanged } // end class def