
/* This applet demonstrates several GUI components that are
   available in Java 1.1.  Note that the class is defined
   to implement the interfaces ActionListener and ItemListener
   so that it can handle events generated by the various
   components.
   
   David Eck
*/

import java.awt.*;         // make GUI compnent classes available
import java.awt.event.*;   // make GUI event classes available
import java.applet.Applet; // make the Applet class available

public class GUIDemo extends Applet implements ItemListener, ActionListener{

   private TextArea transcript;  // a message will be posted to this text area
                                 // each time an event is generated by some
                                 // some user action

   public void init() {  // set up the applet
   
      setBackground(Color.white);
   
      setLayout(new GridLayout(2,1,7,7));
         // I will put the transcript area in the bottom half of the
         // applet.  The top half will be occupied by a grid of
         // four lines.  Each line contains a componenet and
         // a label for that component.

      transcript = new TextArea();
      transcript.setEditable(false);
      Panel top = new Panel();
      top.setLayout(new GridLayout(4,1));
      add(top);
      add(transcript);
      
      Panel line = new Panel();
      line.add(new Label("Push Button:  "));
      Button b = new Button("Click Me!");
      b.addActionListener(this);
      line.add(b);
      top.add(line);
      
      line = new Panel();
      line.add(new Label("Checkbox:  "));
      Checkbox c = new Checkbox("Click me!");
      c.addItemListener(this);
      line.add(c);
      top.add(line);
      
      line = new Panel();
      line.add(new Label("Text Field:  "));
      TextField t = new TextField("Click, type, press return!  ");
      t.addActionListener(this);
      line.add(t);
      top.add(line);

      line = new Panel();
      line.add(new Label("Pop-up Menu:  "));
      Choice m = new Choice();
      m.addItem("First Option");
      m.addItem("Second Option");
      m.addItem("Third Option");
      m.addItem("Fourth Option");
      m.addItemListener(this);
      line.add(m);
      top.add(line);

   }  // end init()
   
   private void post(String message) {  // add a line to the transcript
      transcript.append(message + "\n");
   }
   
   public Insets getInsets() {  // Allow 7 pixels space around the edges of the applet
      return new Insets(7,7,7,7);
   }
   
   public void actionPerformed(ActionEvent evt) {
         // respond to an action event; this method is part of the
         // ActionListener interface.
      Object target = evt.getSource();  // which component produced this event?
      if (target instanceof Button)
         post("Button was clicked.");
      else if (target instanceof TextField)
         post("Pressed return in TextField with contents: " + evt.getActionCommand());
   }
   
   public void itemStateChanged(ItemEvent evt) {
         // respond to an item event; this method is part of the
         // ItemListener interface.
      Object target = evt.getSource();
      if (target instanceof Checkbox) {
          if (evt.getStateChange() == ItemEvent.SELECTED)
             post("Checkbox was turned on.");
          else
             post("Checkbox was turned off.");
      }
      else {
         post("Item \"" + evt.getItem() + "\" selected from pop-up menu.");          
      }
   }
   
}  // end class GUIDemo
