Checkboxes

The following example demonstrates how to use the Checkbox class in a Pivot application. Selecting a checkbox shows or hides an associated icon:

The WTKX source for the example is below:

<Window title="Checkboxes" maximized="true"
    xmlns:wtkx="http://pivot.apache.org/wtkx"
    xmlns="org.apache.pivot.wtk">
    <content>
        <TablePane styles="{showHorizontalGridLines: true, showVerticalGridLines:true,
            padding:4, horizontalSpacing:1, verticalSpacing:1, gridColor:10}">
            <columns>
                <TablePane.Column width="-1"/>
                <TablePane.Column width="24"/>
            </columns>

            <rows>
                <TablePane.Row height="24">
                    <BoxPane styles="{verticalAlignment:'center'}">
                        <Checkbox wtkx:id="bellCheckbox" buttonData="Bell"/>
                    </BoxPane>
                    <ImageView wtkx:id="bellImageView" image="org/apache/pivot/tutorials/bell.png" visible="false"/>
                </TablePane.Row>

                <TablePane.Row height="24">
                    <BoxPane styles="{verticalAlignment:'center'}">
                        <Checkbox wtkx:id="clockCheckbox" buttonData="Clock"/>
                    </BoxPane>
                    <ImageView wtkx:id="clockImageView" image="org/apache/pivot/tutorials/clock.png" visible="false"/>
                </TablePane.Row>

                <TablePane.Row height="24">
                    <BoxPane styles="{verticalAlignment:'center'}">
                        <Checkbox wtkx:id="houseCheckbox" buttonData="House"/>
                    </BoxPane>
                    <ImageView wtkx:id="houseImageView" image="org/apache/pivot/tutorials/house.png" visible="false"/>
                </TablePane.Row>
            </rows>
        </TablePane>
    </content>
</Window>

Note that this example uses the TablePane layout container. TablePane defines a two-dimensional grid structure and is similar to an HTML table. It is discussed in more detail in the Layout Containers section.

The following is the Java source for the example. A button press handler is assigned to each checkbox. When the handler is invoked, it simply toggles the visibility of the corresponding image view, causing the image to appear and disappear based on the checkbox's selection state.

package org.apache.pivot.tutorials.buttons;

import org.apache.pivot.collections.Map;
import org.apache.pivot.wtk.Application;
import org.apache.pivot.wtk.Button;
import org.apache.pivot.wtk.ButtonPressListener;
import org.apache.pivot.wtk.Checkbox;
import org.apache.pivot.wtk.DesktopApplicationContext;
import org.apache.pivot.wtk.Display;
import org.apache.pivot.wtk.ImageView;
import org.apache.pivot.wtk.Window;
import org.apache.pivot.wtkx.WTKXSerializer;

public class Checkboxes implements Application {
    private Window window = null;
    private Checkbox bellCheckbox = null;
    private Checkbox clockCheckbox = null;
    private Checkbox houseCheckbox = null;
    private ImageView bellImageView = null;
    private ImageView clockImageView = null;
    private ImageView houseImageView = null;

    public void startup(Display display, Map<String, String> properties) throws Exception {
        WTKXSerializer wtkxSerializer = new WTKXSerializer();
        window = (Window)wtkxSerializer.readObject(this, "checkboxes.wtkx");
        bellCheckbox = (Checkbox)wtkxSerializer.get("bellCheckbox");
        clockCheckbox = (Checkbox)wtkxSerializer.get("clockCheckbox");
        houseCheckbox = (Checkbox)wtkxSerializer.get("houseCheckbox");
        bellImageView = (ImageView)wtkxSerializer.get("bellImageView");
        clockImageView = (ImageView)wtkxSerializer.get("clockImageView");
        houseImageView = (ImageView)wtkxSerializer.get("houseImageView");

        // Wire up event listeners
        bellCheckbox.getButtonPressListeners().add(new ButtonPressListener() {
            public void buttonPressed(Button button) {
                bellImageView.setVisible(!bellImageView.isVisible());
            }
        });

        clockCheckbox.getButtonPressListeners().add(new ButtonPressListener() {
            public void buttonPressed(Button button) {
                clockImageView.setVisible(!clockImageView.isVisible());
            }
        });

        houseCheckbox.getButtonPressListeners().add(new ButtonPressListener() {
            public void buttonPressed(Button button) {
                houseImageView.setVisible(!houseImageView.isVisible());
            }
        });

        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(Checkboxes.class, args);
    }
}

Next: Link Buttons