Control Size Location : Layout « SWT JFace Eclipse « Java

Control Size Location

Control Size Location
 * Copyright (c) 1998, 2004 Jackwind Li Guojie
 * All right reserved. 
 * Created on Jan 23, 2004 11:26:26 PM by JACK
 * $Id$
 * visit:

import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

public class ControlSizeLocation {
  Display display = new Display();
  Shell shell = new Shell(display);

  Button button;

  Text x;
  Text y;
  Text h;
  Text w;

  Button get;
  Button set;

  public ControlSizeLocation() {


    // Set up the event loop.
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch()) {
        // If no more entries in event queue


  private void init() {
    GridLayout gridLayout = new GridLayout(2, true);

    Composite left = new Composite(shell, SWT.NULL);
    left.setLayout(new GridLayout());
    //left.setLayout(new FillLayout());
    left.setLayoutData(new GridData(GridData.FILL_BOTH));

    button = new Button(left, SWT.PUSH);
    button.setLayoutData(new GridData());

    Composite right = new Composite(shell, SWT.NULL);
    right.setLayout(new GridLayout(4, true));
    right.setLayoutData(new GridData(GridData.FILL_BOTH));

    Label label = new Label(right, SWT.NULL);

    label = new Label(right, SWT.NULL);

    label = new Label(right, SWT.NULL);

    label = new Label(right, SWT.NULL);

    x = new Text(right, SWT.BORDER);
    y = new Text(right, SWT.BORDER);
    w = new Text(right, SWT.BORDER);
    h = new Text(right, SWT.BORDER);

    SelectionListener selectionListener = new SelectionListener() {
      public void widgetSelected(SelectionEvent e) {
        Button b = (Button) e.widget;
        if (b == get) {
          System.out.println("getBounds: " + button.getBounds());
          System.out.println("getLocation: " + button.getLocation());
          System.out.println("getSize: " + button.getSize());
        }else if(b == set) {
          int vx = getNumber(x);
          int vy = getNumber(y);
          int vw = getNumber(w);
          int vh = getNumber(h);
          if(vx != -1 && vy != -1) {
            if(vw != -1 && vh != -1) {
              Rectangle rectangle = new Rectangle(vx, vy, vw, vh);
              System.out.println("# setBounds: " + rectangle);
              Point point = new Point(vx, vy);
              System.out.println("# setLocation: " + point);
          }else if(vw != -1 && vh != -1) {
            Point point = new Point(vw, vh);
            System.out.println("# setSize: " + point);


      public void widgetDefaultSelected(SelectionEvent e) {
        // TODO Auto-generated method stub


    get = new Button(right, SWT.PUSH);

    set = new Button(right, SWT.PUSH);

   * @param text
   * @return <code>-1</code> if invalid.
  private int getNumber(Text text) {
    if(text == null)
      return -1;
    String value = text.getText();
    if(value == null || value.trim().length() == 0)
      return -1;
    try {
      return Integer.parseInt(value.trim());
    }catch(Exception e) {
    return -1;

  public static void main(String[] args) {
    new ControlSizeLocation();


Related examples in the same category

1.SWT Layout Example in OneSWT Layout Example in One
2.SWT GridLayout DemoSWT GridLayout Demo
3.SWT GridLayoutSWT GridLayout
4.SWT RowLayout SampleSWT RowLayout Sample
5.SWT FillLayout SampleSWT FillLayout Sample
6.SWT Form Layout SampleSWT Form Layout Sample
7.SWT GridLayout SampleSWT GridLayout Sample
8.GridLayout Sample Grab SpaceGridLayout Sample Grab Space
9.Grid Layout SpanGrid Layout Span
10.Layout ComponentsLayout Components
11.Layout TermsLayout Terms
12.StackLayout SampleStackLayout Sample
13.BorderLayout SampleBorderLayout Sample
15.FormLayout SimpleFormLayout Simple
16.A BorderLayoutA BorderLayout
17.FillLayout HorizontalFillLayout Horizontal
18.FillLayout VerticalFillLayout Vertical
19.FormLayout ComplexFormLayout Complex
20.FormLayout Form AttachmentFormLayout Form Attachment
22.GridLayout 2x2GridLayout 2x2
23.Grid Layout ComplexGrid Layout Complex
24.No LayoutNo Layout
25.Row Layout TestRow Layout Test
26.Stack Layout TestStack Layout Test
27.Row Layout HorizontalRow Layout Horizontal
28.FillLayout ExampleFillLayout Example
29.FormLayout ExampleFormLayout Example
30.GridLayout ExampleGridLayout Example
31.GridLayout Example 2GridLayout Example 2
32.Layout ExampleLayout Example
33.RowLayout ExampleRowLayout Example
34.SWT FillLayout
35.SWY Radial Layout
36.Use of Radial Layout
37.GridLayout with All Options
38.Simplest GridLayout
39.SWT GridLayout: GridSpan
40.SWT RowLayout
41.SWT FillLayout Composite
42.SWT GridLayout: align widgets in a vertical columnSWT GridLayout: align widgets in a vertical column
43.SWT GridLayout: align widgets in a horizontal rowSWT GridLayout: align widgets in a horizontal row
44.Exclude a widget from a GridLayoutExclude a widget from a GridLayout
45.Insert widgets into a grid layoutInsert widgets into a grid layout
46.Align widgets in a GridLayoutAlign widgets in a GridLayout
47.FormLayout: create a simple OK and CANCEL dialog using form layoutFormLayout: create a simple OK and CANCEL dialog using form layout
48.FormLayout: center a label and single line text using a form layoutFormLayout: center a label and single line text using a form layout
49.FormLayout: create a simple dialog using form layoutFormLayout: create a simple dialog using form layout