Java Article 2 Code: PushMe.java

// PushMe.java
// By Ned Etcode
// Copyright 1995, 1996 Netscape Communications Corp.  All rights reserved.

import netscape_beta.application.*;
import netscape_beta.util.*;

/** In this example we create several user interface elements and place
  * them on the root view. We set ourselves as the target of these
  * elements so that as the user interacts with them we receive the
  * corresponding commands.
  */
public class PushMe extends Application implements Target {
    Button button;
    Slider slider;
    TextField textField;
    TextField statusField;
    GrayView grayView;

    /** This method gets called to initialize an application. We'll take
      * this opportunity to set up the View hierarchy.
      */
    public void init() {
        super.init();

        // Change the color of the root view to be the standard
        // application lightGray.

        mainRootView().setColor(Color.lightGray);

        // Set up a TextField which will display status messages.  The
        // setBuffered(true) call will prevent it from flickering when someone
        // drags the slider.  After setting it up we add it as a subview of
        // the root view so that it will be displayed on the screen.

        statusField = new TextField(24, 24, 256, 24);
        statusField.setBorder(null);
        statusField.setEditable(false);
        statusField.setBackgroundColor(Color.lightGray);
        statusField.setFont(Font.fontNamed("Courier", Font.PLAIN, 12));
        statusField.setBuffered(true);
        statusField.setStringValue("Hello World");
        mainRootView().addSubview(statusField);

        // Create a button with the text "Push Me" on it.  We set ourselves
        // as the target of the button, so that we'll receive a
        // performCommand() message (see the Target interface) with the
        // command "buttonWasPressed" whenever the button is pressed.

        button = new Button(24, 64, 64, 24);
        button.setTitle("Push Me");
        button.setTarget(this);
        button.setCommand("buttonWasPressed");
        mainRootView().addSubview(button);

        // Make a Slider and tell it to send us the command "sliderWasMoved"
        // whenever the slider changes.

        slider = new Slider(24, 96, 64, 24);
        slider.setTarget(this);
        slider.setCommand("sliderWasMoved");
        slider.setBuffered(true);
        mainRootView().addSubview(slider);

        // Make a TextField which the user can edit.  The API to set the
        // target and command of a TextField will improve in a future release.

        textField = new TextField(24, 128, 64, 24);
        textField.setCommand("textWasEntered");
        textField.setTarget(this);
        mainRootView().addSubview(textField);

        // GrayView is a custom View subclass which just draws solid gray
        // rectangle. We change the shade of gray it displays in the
        // sliderWasMoved() method below.

        grayView = new GrayView(128, 64, 128, 128);
        mainRootView().addSubview(grayView);
    }

    /** This is the sole method of the Target interface. Since java doesn't
      * have a way to do callbacks, this ugly little bit of code needs to
      * exist. The convention we recommend is to simply invoke the method
      * with the same name as the command.
      */
    public void performCommand(String command, Object arg) {
        if (command.equals("buttonWasPressed"))
            buttonWasPressed(arg);
        else if (command.equals("sliderWasMoved"))
            sliderWasMoved(arg);
        else if (command.equals("textWasEntered"))
            textWasEntered(arg);
    }

    /** This method is invoked when the button is pressed.
      */
    public void buttonWasPressed(Object arg) {
        showStatus("Button Was Pressed!");
    }

    /** This method is invoked when the slider moves. We set the shade of
      * gray displayed in the GrayView from the value of the slider.
      */
    public void sliderWasMoved(Object arg) {
        grayView.setValue(slider.value());
        showStatus("Slider Was Moved!  value = " + slider.value());
    }

    /** This method is invoked when the user hits return in the TextField.
      */
    public void textWasEntered(Object arg) {
        showStatus("Text Was Entered!  value = \"" +
                    textField.stringValue() + "\"");
    }

    /** We call showStatus() from several places to update the text displayed
      * in the status field.
      */
    public void showStatus(String status) {
        statusField.setStringValue(status);
    }

    // This method allows PushMe to run as a stand alone application.
    public static void main(String args[]) {
        PushMe app;
        ExternalWindow mainWindow;
        Size size;

        app = new PushMe();
        mainWindow = new ExternalWindow();
        app.setMainRootView(mainWindow.rootView());
        size = mainWindow.windowSizeForContentSize(320, 200);
        mainWindow.sizeTo(size.width, size.height);
        mainWindow.show();

        app.run();
    }
}
Back to IFC article