Sunday 18 December 2016

SWT: Progress Bar widget

You can define Progress bar widget using ProgressBar class.

Following statements define horizontal progress bar.

ProgressBar horizontalProgressBar = new ProgressBar(shell, SWT.HORIZONTAL);
horizontalProgressBar.setMaximum(100);
horizontalProgressBar.setMinimum(10);
horizontalProgressBar.setBounds(50, 50, horizontalScaleWidth, horizontalScaleHeight);
horizontalProgressBar.setBackground(new Color(display, 255, 255, 255));
horizontalProgressBar.setSelection(30);

Following statements define vertical progress bar.

ProgressBar verticalProgressBar = new ProgressBar(shell, SWT.VERTICAL);
verticalProgressBar.setMaximum(1000);
verticalProgressBar.setMinimum(500);
verticalProgressBar.setBounds(250, 150, verticalScaleWidth, verticalScaleHeight);
verticalProgressBar.setBackground(new Color(display, 255, 255, 255));
verticalProgressBar.setSelection(650);

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.ProgressBar;
import org.eclipse.swt.widgets.Shell;

public class Test {

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

 private static int horizontalScaleWidth = 600;
 private static int horizontalScaleHeight = 50;

 private static int verticalScaleWidth = 50;
 private static int verticalScaleHeight = 400;

 private static void addWidgetsToShell(Display display, Shell shell) {

  ProgressBar horizontalProgressBar = new ProgressBar(shell, SWT.HORIZONTAL);
  horizontalProgressBar.setMaximum(100);
  horizontalProgressBar.setMinimum(10);
  horizontalProgressBar.setBounds(50, 50, horizontalScaleWidth, horizontalScaleHeight);
  horizontalProgressBar.setBackground(new Color(display, 255, 255, 255));
  horizontalProgressBar.setSelection(30);

  ProgressBar verticalProgressBar = new ProgressBar(shell, SWT.VERTICAL);
  verticalProgressBar.setMaximum(1000);
  verticalProgressBar.setMinimum(500);
  verticalProgressBar.setBounds(250, 150, verticalScaleWidth, verticalScaleHeight);
  verticalProgressBar.setBackground(new Color(display, 255, 255, 255));
  verticalProgressBar.setSelection(650);

 }

 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