Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set rows and columns of a widget in grid layout?

Tags:

swt

I am using grid layout in a shell. I want to control the location of widgets. As you can see the bottom buttons have to be on row 4. I am using windowbuilder plugin and it can place them in desired places.

my shell using windowbuilder

The problem is I can't see the placement instruction in source code. How can I place them the programmatic way. Here is the source code window builder generates.

package dgsj.Chapter04.examples.ch4;

import org.eclipse.swt.widgets.*;

public class GridLayout2x2 {
    private static GridData data_1;
    private static GridData data_2;

    public static void main(String[] args) {
        Display display = new Display();
        Shell shell = new Shell(display);
        shell.setMinimumSize(new Point(123, 30));
        shell.setSize(126, 123);
        GridLayout layout = new GridLayout();
        layout.numColumns = 3;
        shell.setLayout(layout);

        GridData data = new GridData(GridData.FILL_BOTH);
        data.grabExcessHorizontalSpace = false;
        data.widthHint = 200;
        data.grabExcessVerticalSpace = false;
        Button one = new Button(shell, SWT.PUSH);
        one.setText("one");
        one.setLayoutData(data);

        data = new GridData(GridData.FILL_BOTH);
        Button two = new Button(shell, SWT.PUSH);
        two.setText("two");
        two.setLayoutData(data);
        new Label(shell, SWT.NONE);
        new Label(shell, SWT.NONE);

        data_2 = new GridData(GridData.FILL_BOTH);
        Button four = new Button(shell, SWT.PUSH);
        four.setText("four");
        four.setLayoutData(data_2);
        new Label(shell, SWT.NONE);
        new Label(shell, SWT.NONE);
        new Label(shell, SWT.NONE);

        new Label(shell, SWT.NONE);
        new Label(shell, SWT.NONE);

        Button btnNewButton = new Button(shell, SWT.NONE);
        btnNewButton.setText("New Button");

        data_1 = new GridData(GridData.FILL_BOTH);
        data_1.grabExcessVerticalSpace = false;
        data_1.grabExcessHorizontalSpace = false;
        Button three = new Button(shell, SWT.PUSH);

        three.setText("three");
        three.setLayoutData(data_1);

        shell.pack();
        shell.open();
        while (!shell.isDisposed()) {
            if (!display.readAndDispatch()) {
                display.sleep();
            }
        }
        display.dispose();
    }
}
like image 723
morteza khosravi Avatar asked Jun 19 '13 13:06

morteza khosravi


2 Answers

The only way to "skip" some cells in GridLayout is to fill them with empty controls, and that's what WindowBuilder does with those new Label(shell, SWT.NONE); lines. If you want to place a control on given line/column programmatically, you'll have to count the required number of empty Labels (or other invisible controls) to add.

like image 69
Alexey Romanov Avatar answered Oct 25 '22 06:10

Alexey Romanov


You'd better use FormLayout, which is more flexible than GridLayout. If you need it, you can get a widget FormData, change its settings and then force a re-layout to reposition the widgets at runtime.

Eclipse's SWT Layout view might be of great help for you.

Here's some code that reproduces your layout:

import org.eclipse.swt.*;
import org.eclipse.swt.layout.*;
import org.eclipse.swt.widgets.*;

public class MyLayout {
    public static void main (String [] args) {
        Display display = new Display ();
        Shell shell = new Shell (display);
        FormLayout formLayout = new FormLayout ();
        shell.setLayout (formLayout);

        Button button0 = new Button (shell, SWT.PUSH);
        button0.setText ("button0");
        FormData data = new FormData ();
        data.left = new FormAttachment (0, 0);
        data.right = new FormAttachment (button1, 0, SWT.DEFAULT);
        button0.setLayoutData (data);

        Button button1 = new Button (shell, SWT.PUSH);
        button1.setText ("button1");
        data = new FormData ();
        data.left = new FormAttachment (button3, 0, SWT.LEFT);
        data.right = new FormAttachment (button3, 0, SWT.RIGHT);
        button1.setLayoutData (data);

        Button button2 = new Button (shell, SWT.PUSH);
        button2.setText ("button2");
        data = new FormData ();
        data.left = new FormAttachment (button3, 0, SWT.LEFT);
        data.right = new FormAttachment (button3, 0, SWT.RIGHT);
        data.top = new FormAttachment (button1, 0, SWT.DEFAULT);
        button2.setLayoutData (data);

        Button button3 = new Button (shell, SWT.PUSH);
        button3.setText ("button3");
        data = new FormData ();
        data.right = new FormAttachment (button4, 0, SWT.DEFAULT);
        data.bottom = new FormAttachment (100, 0);
        button3.setLayoutData (data);

        Button button4 = new Button (shell, SWT.PUSH);
        button4.setText ("button4");
        data = new FormData ();
        data.right = new FormAttachment (100, 0);
        data.bottom = new FormAttachment (100, 0);
        button4.setLayoutData (data);

        shell.pack ();
        shell.open ();

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

        display.dispose ();
    }
}
like image 45
Mario Marinato Avatar answered Oct 25 '22 08:10

Mario Marinato