o7planning

Java SWT Slider Tutorial with Examples

View more Tutorials:

Follow us on our fanpages to receive notifications every time there are new articles. Facebook Twitter

1- SWT Slider

Slider is a UI component allowing you to select a numeric value in a range of values. The slider includes a track and draggable thumb.
Block increment: The value increases or decreases when you click the left arrow or right arrow in order to move the thumb.

final Slider slider = new Slider(shell, SWT.HORIZONTAL);

slider.setMinimum(100);
slider.setMinimum(20);
slider.setSelection(30);
slider.setIncrement(10);

2- JavaFX Slider Example

Slider view in Windows 8:
Slider view in Windows XP:
SliderDemo.java

package org.o7planning.swt.slider;

import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Slider;

public class SliderDemo {
    public static void main(String[] args) {
        Display display = new Display();
        Shell shell = new Shell(display);
        shell.setText("SWT Slider (o7planning.org)");
        shell.setSize(400, 200);
        shell.setLayout(null);

        final Slider slider = new Slider(shell, SWT.HORIZONTAL);
        slider.setMinimum(100);
        slider.setMinimum(20);
        slider.setSelection(30);
        slider.setIncrement(10); 
        slider.setBounds(20, 30, 286, 15);
        final Label text = new Label(shell, SWT.NONE);
        text.setBounds(20, 100, 286, 15);

        slider.addListener(SWT.Selection, new Listener() {
            @Override
            public void handleEvent(Event event) {
                String logText = "Event: SWT.NONE";
                switch (event.detail) {
                case SWT.ARROW_DOWN:
                    logText = "Event: SWT.ARROW_DOWN";
                    break;
                case SWT.ARROW_UP:
                    logText = "Event: SWT.ARROW_UP";
                    break;
                case SWT.DRAG:
                    logText = "Event: SWT.DRAG";
                    break;
                case SWT.END:
                    logText = "Event: SWT.END";
                    break;
                case SWT.HOME:
                    logText = "Event: SWT.HOME";
                    break;
                case SWT.PAGE_DOWN:
                    logText = "Event: SWT.PAGE_DOWN";
                    break;
                case SWT.PAGE_UP:
                    logText = "Event: SWT.PAGE_UP";
                    break;
                }
                logText += " Position: " + slider.getSelection();
                text.setText(logText);
            }
        });
        shell.open();
        while (!shell.isDisposed()) {
            if (!display.readAndDispatch())
                display.sleep();
        }
        display.dispose();
    }
}

View more Tutorials:

Maybe you are interested

These are online courses outside the o7planning website that we introduced, which may include free or discounted courses.