Push Buttons

Below is an example of a Pivot PushButton. Clicking the button opens a simple modal dialog informing the user that the button was clicked:

The WTKX source for the example is below:

<Window title="Push Buttons" maximized="true"
    xmlns:wtkx="http://pivot.apache.org/wtkx"
    xmlns="org.apache.pivot.wtk">
    <content>
        <BoxPane styles="{padding:4, horizontalAlignment:'center',
            verticalAlignment:'center'}">
            <PushButton wtkx:id="pushButton" buttonData="Click Me!"/>
        </BoxPane>
    </content>
</Window>

The following is the Java source for the example. The application simply registers the event listener that is called when the button is pressed, as an anonymous inner class that implements ButtonPressListener:

package org.apache.pivot.tutorials.buttons;

import org.apache.pivot.collections.Map;
import org.apache.pivot.wtk.Alert;
import org.apache.pivot.wtk.Application;
import org.apache.pivot.wtk.Button;
import org.apache.pivot.wtk.ButtonPressListener;
import org.apache.pivot.wtk.DesktopApplicationContext;
import org.apache.pivot.wtk.Display;
import org.apache.pivot.wtk.MessageType;
import org.apache.pivot.wtk.PushButton;
import org.apache.pivot.wtk.Window;
import org.apache.pivot.wtkx.WTKXSerializer;

public class PushButtons implements Application {
    private Window window = null;
    private PushButton pushButton = null;

    public void startup(Display display, Map<String, String> properties) throws Exception {
        WTKXSerializer wtkxSerializer = new WTKXSerializer();
        window = (Window)wtkxSerializer.readObject(this, "push_buttons.wtkx");
        pushButton = (PushButton)wtkxSerializer.get("pushButton");

        // Add a button press listener
        pushButton.getButtonPressListeners().add(new ButtonPressListener() {
            public void buttonPressed(Button button) {
                Alert.alert(MessageType.INFO, "You clicked me!", window);
            }
        });

        window.open(display);
    }

    public boolean shutdown(boolean optional) {
        if (window != null) {
            window.close();
        }

        return false;
    }

    public void suspend() {
    }

    public void resume() {
    }

    public static void main(String[] args) {
        DesktopApplicationContext.main(PushButtons.class, args);
    }
}

When called, the event handler method, buttonPressed(), uses a static method of the Alert class to display the message to the user.

Next: Toggle Buttons