Wednesday, 14 December 2016

SWT: Slider

Slider class is used to define Slider widger.

Ex:
Slider horizontalSlider = new Slider(shell, SWT.HORIZONTAL);
horizontalSlider.setMaximum(100);
horizontalSlider.setMinimum(10);
horizontalSlider.setBounds(50, 50, horizontalSliderWidth, horizontalSliderHeight);
horizontalSlider.setThumb(20);
horizontalSlider.setBackground(new Color(display, 255, 255, 255));

Following is the complete working application.

package swt_app;

import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Slider;

public class Test {

 private static int shellWidth = 1000;
 private static int shellHeight = 700;

 private static int horizontalSliderWidth = 600;
 private static int horizontalSliderHeight = 50;

 private static int verticalSliderWidth = 50;
 private static int verticalSliderHeight = 500;

 private static void addWidgetsToShell(Display display, Shell shell) {
  Slider horizontalSlider = new Slider(shell, SWT.HORIZONTAL);
  horizontalSlider.setMaximum(100);
  horizontalSlider.setMinimum(10);
  horizontalSlider.setBounds(50, 50, horizontalSliderWidth, horizontalSliderHeight);
  horizontalSlider.setThumb(20);
  horizontalSlider.setBackground(new Color(display, 255, 255, 255));

  Slider verticalSlider = new Slider(shell, SWT.VERTICAL);
  verticalSlider.setMaximum(1000);
  verticalSlider.setMinimum(500);
  verticalSlider.setBounds(250, 150, verticalSliderWidth, verticalSliderHeight);
  verticalSlider.setThumb(20);
  verticalSlider.setBackground(new Color(display, 255, 255, 255));

 }

 public static void main(String[] args) {

  /* Instantiate Display object, it represents SWT session */
  Display display = new Display();

  /*
   * Define Shell, it represent a window, You can add more than one shell
   * to Display
   */
  Shell shell = new Shell(display);
  shell.setSize(shellWidth, shellHeight);

  addWidgetsToShell(display, shell);

  /* Open shell window */
  shell.open();

  /*
   * Run the event dispatching loop until an exit condition occurs, which
   * is typically when the main shell window is closed by the user.
   */

  while (!shell.isDisposed()) {
   if (!display.readAndDispatch())
    display.sleep();
  }

  /* Dispose the display */
  display.dispose();

 }
}



Previous                                                 Next                                                 Home

No comments:

Post a Comment