Create Scale in SWT

In order to create the scale, SWT provides the class Scale of package org.eclipse.swt.widgets.*.

Create Scale in SWT


In this section, you will study how to create scale.

In order to create the scale, SWT provides the class Scale of package org.eclipse.swt.widgets.*. This class shows the different ranges of numeric values. The class Label allows to display the text. The class GridData sets the layout of the scale.

The class Text provides the textbox and allows to enter the text. The method setText() set the content to the specified string. The method setEditable(true) sets the editable state to modify the text.

The SWT.HORIZONTAL creates the horizontal scale. The method setMaximum() sets the maximum value of scale and setMinimum() sets the minimum value. The method setIncrement() sets the increment value. The method setPageIncrement() sets the page increment. The method getMinimum() returns the minimum value and getMaximum() returns the maximum value. The method getSelection() returns the selected value. The method addListener() calls the Event class to display the selected value from the scale to the textbox.

Following code displays the selected value:

int value = scale.getMaximum() - scale.getSelection() + scale.getMinimum();
System.out.println("Speed: " + value);
text.setText("Speed: " + value);

 Here is the code of

import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.*;
import org.eclipse.swt.widgets.*;

public class ScaleExample {
  Display display = new Display();
  Shell shell = new Shell(display);
  Scale scale;
  Text text;
 Label label;
  public ScaleExample() {
  shell.setLayout(new GridLayout(1true));
  shell.setText("Show scale");
  label = new Label(shell, SWT.HORIZONTAL);

  scale = new Scale(shell, SWT.HORIZONTAL);
  scale.addListener(SWT.Selection, new Listener() {
  public void handleEvent(Event event) {
  int value = scale.getMaximum() - scale.getSelection() + 

  System.out.println("Speed: " + value);
  text.setText("Speed: " + value);
  text = new Text(shell, SWT.BORDER | SWT.SINGLE);
  scale.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_CENTER)); 
  while (!shell.isDisposed()) {
  if (!display.readAndDispatch()) {
  public static void main(String[] args) {
  new ScaleExample();

Output will be displayed as:

Download Source Code





We have 1000s of tutorials on our website. Search Tutorials tutorials on our website.

Share on Google+Share on Google+

Create Scale in SWT

Posted on: October 3, 2008 If you enjoyed this post then why not add us on Google+? Add us to your Circles



Discuss: Create Scale in SWT  

Post your Comment

Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image