import java.awt.*;
import java.awt.event.*;
import javax.swing.*;        

/**
 *  Creates and displays a simple GUI using Java Swing
 *  Capable of running as standalone application,
 *  or as an applet in a web page.
 *
 *  @author  Nick Howe
 *  @version 17 September 2008
 */
public class GUIdemoCombo extends JApplet {
    /** Label is part of GUI state because we will change its text */
    JLabel label;

    /**
     * Create the GUI and show it.  For thread safety,
     * this method should be invoked from the
     * event-dispatching thread.
     */
    public void createAndShowGUI() {
        // Make sure we have nice window decorations.
        JFrame.setDefaultLookAndFeelDecorated(true);

        // Create and set up the window.
        JFrame frame = new JFrame("JCircleGUIdemoCombo Application");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	frame.getContentPane().setLayout(new GridLayout(2,2));

	// Add components
	createComponents(frame.getContentPane());

        // Display the window.
        frame.pack();
        frame.setVisible(true);
    }

    /**
     *  Creates and lays out all the components for the GUI
     *
     *  @param pane  The container where everything is to be created
     */
    public void createComponents(Container pane) {
	// create and lay out the components
	pane.setLayout(new FlowLayout());
	JButton button = new JButton("Test");
	ToggleListener tlistener = new ToggleListener();
	button.addActionListener(tlistener);
	pane.add(button);
	label = new JLabel("Off");
	pane.add(label);
    }

    /** 
     *  This is the entry point for the applet version 
     */
    public void init() {
        final GUIdemoCombo GUI = new GUIdemoCombo();

	//Execute a job on the event-dispatching thread:
	//creating this applet's GUI.
	try {
	    javax.swing.SwingUtilities.invokeAndWait(new Runnable() {
		    public void run() {
                        // line below would create separate window
			//gui.createAndShowGUI();

                        // this line creates applet in browser window
                        GUI.createComponents(getContentPane());
		    }
		});
	} catch (Exception e) {
	    System.err.println("createGUI didn't successfully complete");
	}
    }

    /** 
     *  This is the entry point for the application
     */
    public static void main(String[] args) {
        // Schedule a job for the event-dispatching thread:
        // creating and showing this application's GUI.
	final GUIdemoCombo GUI = new GUIdemoCombo();

        javax.swing.SwingUtilities.invokeLater(new Runnable() {
		public void run() {
		    GUI.createAndShowGUI();
		}
	    });
    }

    /** This is the nested listener class for the toggle button */
    private class ToggleListener implements ActionListener {
	/** 
	 *  When the button is clicked, toggle the label state
	 *
	 *  @param e  Describes the event
	 */
	public void actionPerformed(ActionEvent e) {
	    if (label.getText().equals("On")) {
		label.setText("Off");
	    } else {
		label.setText("On");
	    }
	}
    }
}
