  // A Font Choose Applet

  import java.applet.*;
  import java.awt.*;
  import java.awt.event.*;

  public class FontChooserApplet extends Applet implements AdjustmentListener, ItemListener
  {
	// logical fonts
	final static String[] fonts = {"Serif", "SansSerif", "Monospaced", "Dialog", "DialogInput" };

	private Scrollbar fontSize;
	private Label theSample;
	private Choice theFont;
	private List theStyle;
	private Label theStatus;
	
	public void init()
	{
	  //initialise the components

	  this.theFont = new Choice();
	  this.theSample = new Label("The Sample Text");
	  this.fontSize = new Scrollbar(Scrollbar.HORIZONTAL, 10, 5, 0, 100);
	  this.theStyle = new List(3, true);
	  this.theStatus = new Label("Select a font:");

	  //add the listeners
	  this.theFont.addItemListener(this);
	  this.fontSize.addAdjustmentListener(this);
	  this.theStyle.addItemListener(this);

	  //lay out the components	
	  this.setLayout(new BorderLayout());
	  this.add("North",theStatus);

	  Panel mainPanel = new Panel(new GridLayout(1,2));
		
	  Panel rightPanel = new Panel(new GridLayout(3,2));
	  rightPanel.add(new Label("Font Name"));
	  rightPanel.add(this.theFont);
	  rightPanel.add(new Label("Font Size"));
	  rightPanel.add(this.fontSize);
	  rightPanel.add(new Label("Font Style"));
	  rightPanel.add(this.theStyle);
	  
	  mainPanel.add(this.theSample);
	  mainPanel.add(rightPanel);

	  this.add("Center", mainPanel);

	  //populate the Choice and List components

	  for(int i=0; i<fonts.length; i++)  //add logical fonts only
	  {
		theFont.add(fonts[i]);
	  }
	  this.theStyle.add("Italics");
	  this.theStyle.add("Bold");
	}
	
	public void adjustmentValueChanged(AdjustmentEvent e)
	{
	  if (e.getSource().equals(this.fontSize))
	  {
		this.updateSampleText();
	  }
	}

	public void itemStateChanged(ItemEvent e)
	{
	  if (e.getSource().equals(this.theFont) || e.getSource().equals(this.theStyle))
	  {
		this.updateSampleText();
	  }
	}

	private void updateSampleText()
	{
	   String theCurrentFont = theFont.getSelectedItem();
	   int size = fontSize.getValue();
	   int style =0;
	   int theStyleIndexes[] = this.theStyle.getSelectedIndexes();

	   for(int i=0; i<theStyleIndexes.length; i++)
	   {
		if (theStyleIndexes[i] == 0) style+= Font.ITALIC;
		if (theStyleIndexes[i] == 1) style+= Font.BOLD;
	   }
	
	   Font f = new Font(theCurrentFont,style,size);
	   this.theSample.setFont(f);

	   this.theStatus.setText("Font: "+theCurrentFont+" Size: "+size + " Style: "+ style);
	}
  }