mydomain
No ADS
No ADS

Java SWT Scale Tutorial with Examples

  1. SWT Scale
  2. SWT Scale Example

1. SWT Scale

SWT Scale is interface component which allows the user to select a numeric value in a set of continuous ones.
Below is 2 Scales including the vertical scale and the horizontal one.
SWT Scale contains ticks marking its continuous value. These ticks are equally spaced.

2. SWT Scale Example

No ADS
ScaleDemo.java
package org.o7planning.swt.scale;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Scale;
import org.eclipse.swt.widgets.Shell;

public class ScaleDemo {
    public ScaleDemo() {
        Display display = new Display();
        Shell shell = new Shell(display);
        shell.setText("SWT Scale (o7planning.org)");
        shell.setSize(400, 270);

        shell.setLayout(new GridLayout(1, true));

        Label label = new Label(shell, SWT.NULL);
        label.setText("Brightness:"); 
        // Scale
        Scale scale = new Scale(shell, SWT.VERTICAL);
        scale.setMaximum(100);
        scale.setMinimum(0);
        scale.setIncrement(1);
        scale.setPageIncrement(5);
        // Info
        Label labelInfo = new Label(shell, SWT.NONE | SWT.CENTER);
        GridData gd = new GridData(40, SWT.DEFAULT); 
        labelInfo.setLayoutData(gd);

        // Event
        scale.addSelectionListener(new SelectionAdapter() {
            @Override
            public void widgetSelected(SelectionEvent e) {
                int value = scale.getMaximum() - scale.getSelection() + scale.getMinimum();
                labelInfo.setText("" + value);
            }
        });
        shell.open();
        while (!shell.isDisposed()) {
            if (!display.readAndDispatch()) {
                display.sleep();
            }
        }
        display.dispose();
    }
    public static void main(String[] args) {
        new ScaleDemo();
    }
}
No ADS
No ADS