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 BXML source for the example is below:
<buttons:PushButtons title="Push Buttons" maximized="true" xmlns:bxml="http://pivot.apache.org/bxml" xmlns:buttons="org.apache.pivot.tutorials.buttons" xmlns="org.apache.pivot.wtk"> <Border> <BoxPane styles="{padding:4, horizontalAlignment:'center', verticalAlignment:'center'}"> <PushButton bxml:id="pushButton" buttonData="Click Me!"/> </BoxPane> </Border> </buttons:PushButtons>
Note the use of the custom "buttons" namespace. This namespace is associated with the org.apache.pivot.tutorials.buttons package. The root element, <buttons:PushButtons> represents an instance of org.apache.pivot.tutorials.buttons.PushButtons - a custom, application-specific subclass of the org.apache.pivot.wtk.Window class. When the BXML file is loaded, an instance of this class will be returned.
The source code for the PushButtons class is shown below:
package org.apache.pivot.tutorials.buttons; import java.net.URL; import org.apache.pivot.beans.Bindable; import org.apache.pivot.collections.Map; import org.apache.pivot.util.Resources; import org.apache.pivot.wtk.Alert; import org.apache.pivot.wtk.Button; import org.apache.pivot.wtk.ButtonPressListener; import org.apache.pivot.wtk.MessageType; import org.apache.pivot.wtk.PushButton; import org.apache.pivot.wtk.Window; public class PushButtons extends Window implements Bindable { private PushButton pushButton; @Override public void initialize(Map<String, Object> namespace, URL location, Resources resources) { pushButton = (PushButton)namespace.get("pushButton"); pushButton.getButtonPressListeners().add(new ButtonPressListener() { @Override public void buttonPressed(Button button) { Alert.alert(MessageType.INFO, "You clicked me!", PushButtons.this); } }); } }
Note that the PushButtons class implements the org.apache.pivot.beans.Bindable interface. This interface defines a single method, initialize(), that is called when the root element of a BXML file has been completely loaded. This allows the bound class to perform additional initialization tasks such as event registration.
In the initialize() method, the application registers an event listener that is called when the button is pressed. When this listener is invoked, the buttonPressed() event handler method calls a static method of the Alert class to display a simple message to the user.
Next: Toggle Buttons