Radio Buttons

The following example demonstrates how to use the RadioButton class in a Pivot application:

The WTKX source for the example is below:

<Window title="Radio Buttons" maximized="true"
    xmlns:wtkx="http://pivot.apache.org/wtkx"
    xmlns="org.apache.pivot.wtk">
    <content>
        <BoxPane orientation="vertical" styles="{padding:4}">
            <RadioButton wtkx:id="oneButton" buttonData="One" group="numbers" selected="true"/>
            <RadioButton wtkx:id="twoButton" buttonData="Two" group="numbers"/>
            <RadioButton wtkx:id="threeButton" buttonData="Three" group="numbers"/>
            <PushButton wtkx:id="selectButton" buttonData="Select"/>
        </BoxPane>
    </content>
</Window>

The following is the Java source for the example. Like the push button example, the application registers an event listener that is called when the button is pressed. It also gets a reference to the button group, which is used by the handler. This is an instance of Button.Group that is automatically created by the WTKX serializer, which creates one Group per unique button group name. The loader ensures that group names are unique within the application that defines them. Button groups can also be created and assigned programmatically.

numbersGroup is defined as a final local variable so the handler method will have access to it. When called, the handler gets a reference to the currently selected button from the button group and displays an alert containing the data of the selected button.

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 RadioButtons implements Application {
    private Window window = null;
    private PushButton selectButton = null;

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

        // Get a reference to the button group
        final Button.Group numbersGroup = Button.getNamedGroups().get("numbers");

        // Add a button press listener
        selectButton.getButtonPressListeners().add(new ButtonPressListener() {
            public void buttonPressed(Button button) {
                String message = "You selected \""
                    + numbersGroup.getSelection().getButtonData()
                    + "\".";
                Alert.alert(MessageType.INFO, message, 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(RadioButtons.class, args);
    }
}

Next: Checkboxes