Home Tutorials Swt Create Scale in SWT
Questions:Ask|Latest

 
 

Share on Google+Share on Google+

Create Scale in SWT

Advertisement
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 ScaleExample.java

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);
  label.setText("Speed:");

  scale = new Scale(shell, SWT.HORIZONTAL);
  scale.setMinimum(0);
  scale.setMaximum(50);
  scale.setIncrement(1);
  scale.setPageIncrement(10);
  
  scale.addListener(SWT.Selection, new Listener() {
  public void handleEvent(Event event) {
  int value = scale.getMaximum() - scale.getSelection() + 
  scale.getMinimum();

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

Output will be displayed as:

Download Source Code

     

Advertisements

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: October 3, 2008

Related Tutorials

Ask Questions?    Discuss: Create Scale in SWT  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments
DMCA.com