Radio Buttons

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

The BXML source for the example is below:

            <buttons:RadioButtons title="Radio Buttons" maximized="true"
                    <BoxPane orientation="vertical" styles="{padding:4}">
                            <ButtonGroup bxml:id="numbers"/>
                        <RadioButton bxml:id="oneButton" buttonData="One" buttonGroup="$numbers" selected="true"/>
                        <RadioButton bxml:id="twoButton" buttonData="Two" buttonGroup="$numbers"/>
                        <RadioButton bxml:id="threeButton" buttonData="Three" buttonGroup="$numbers"/>
                        <PushButton bxml:id="selectButton" buttonData="Select"/>

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 a button group named numbersGroup (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.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.ButtonGroup;
            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 RadioButtons extends Window implements Bindable {
                private PushButton selectButton = null;

                public void initialize(Map<String, Object> namespace, URL location, Resources resources) {
                    selectButton = (PushButton)namespace.get("selectButton");

                    // Get a reference to the button group
                    final ButtonGroup numbersGroup = (ButtonGroup)namespace.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, RadioButtons.this);

Next: Checkboxes