Sliders

Sliders allow a user to interactively select one of a range of values by dragging the mouse. The following example demonstrates the use of the Slider component. It allows the user to select a value from 0 - 255 and displays the value in a Label component:

The WTKX source for the example is as follows:

<Window title="Sliders" maximized="true"
    xmlns:wtkx="http://pivot.apache.org/wtkx"
    xmlns="org.apache.pivot.wtk">
    <content>
        <BoxPane styles="{verticalAlignment:'center'}">
            <Slider wtkx:id="slider" range="{start:0, end:255}" value="0"/>
            <Label wtkx:id="label"/>
        </BoxPane>
    </content>
</Window>

The Java source loads the WTKX and attaches a SliderValueListener to the slider. When the slider value changes, the updateLabel() method is called to set the current value:

package org.apache.pivot.tutorials.boundedrange;

import org.apache.pivot.collections.Map;
import org.apache.pivot.wtk.Application;
import org.apache.pivot.wtk.DesktopApplicationContext;
import org.apache.pivot.wtk.Display;
import org.apache.pivot.wtk.Label;
import org.apache.pivot.wtk.Slider;
import org.apache.pivot.wtk.SliderValueListener;
import org.apache.pivot.wtk.Window;
import org.apache.pivot.wtkx.WTKXSerializer;

public class Sliders implements Application {
    private Window window = null;
    private Slider slider = null;
    private Label label = null;

    public void startup(Display display, Map<String, String> properties) throws Exception {
        WTKXSerializer wtkxSerializer = new WTKXSerializer();
        window = (Window)wtkxSerializer.readObject(this, "sliders.wtkx");
        slider = (Slider)wtkxSerializer.get("slider");
        label = (Label)wtkxSerializer.get("label");

        slider.getSliderValueListeners().add(new SliderValueListener() {
            @Override
            public void valueChanged(Slider slider, int previousValue) {
                updateLabel();
            }
        });

        updateLabel();
        window.open(display);
    }

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

        return false;
    }

    public void suspend() {
    }

    public void resume() {
    }

    private void updateLabel() {
        label.setText(Integer.toString(slider.getValue()));
    }

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

Next: Spinners