Saturday, 19 November 2016

SWT: Label Widget

Labels are used to display string (or) an image. You can apply following styles to labels.

Constant
Description
SWT.SEPARATOR
Style constant for line separator behavior
SWT.HORIZONTAL
Style constant for horizontal alignment or orientation behavior
SWT.VERTICAL
Style constant for vertical alignment or orientation behavior
SWT.SHADOW_IN
Style constant for shadow in behavior
SWT.SHADOW_OUT
Style constant for shadow out behavior
SWT.SHADOW_NONE
Style constant for no shadow behavior
SWT.CENTER
Style constant for align center behavior
SWT.LEFT
Style constant for align left behavior
SWT.RIGHT
Style constant for align right behavior
SWT.WRAP
Style constant for automatic line wrap behavior

Only one of SHADOW_IN, SHADOW_OUT and SHADOW_NONE may be specified.
Only one of HORIZONTAL and VERTICAL may be specified.
Only one of CENTER, LEFT and RIGHT may be specified.

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

public class Test {

 private static int xPosition = 30;
 private static int yPosition = 30;
 private static int width = 500;
 private static int height = 20;

 private static int shellWidth = 600;
 private static int shellHeight = 300;

 private static void addSeparator(Display display, Shell shell) {
  Label sep1 = new Label(shell, SWT.SEPARATOR);
  sep1.setBounds(30, 60, 100, 20);
  sep1.setSize(500, 2);
  sep1.setLocation(xPosition, yPosition);
  sep1.setBackground(new Color(display, 200, 111, 50));
 }

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

  Label labelCenter = new Label(shell, SWT.CENTER);
  labelCenter.setText("Label Center");
  labelCenter.setBounds(shell.getClientArea());
  labelCenter.setSize(width, height);
  labelCenter.setLocation(xPosition, yPosition);
  yPosition += 30;

  addSeparator(display, shell);
  yPosition += 30;

  Label labelLeft = new Label(shell, SWT.LEFT);
  labelLeft.setText("Label left");
  labelLeft.setBounds(shell.getClientArea());
  labelLeft.setSize(width, height);
  labelLeft.setLocation(xPosition, yPosition);
  yPosition += 30;

  addSeparator(display, shell);
  yPosition += 30;

  Label labelRight = new Label(shell, SWT.RIGHT);
  labelRight.setText("Label Right");
  labelRight.setBounds(shell.getClientArea());
  labelRight.setSize(width, height);
  labelRight.setLocation(xPosition, yPosition);
  yPosition += 30;

 }

 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);

  addLabelsToShell(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