Question

I am facing a small issue in gridlayout. I have one one label and text in one row and another label in the next row. the label in the second row having more length than the label in the first label in the first row. Now I want to position the text field in the first row near to the label1. Since the labe2 has higher characters than label1 it takes the column length of label2. due to that the textfield doesnt come near to the label1. Is it possible to make the textfield near to the label?

Please find the code below..

package zx.example.hello;

import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
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 GridLayoutExample {

        public static void main(String[] args) {
            Display display = new Display();
            Shell shell = new Shell(display);
            GridLayout gridLayout = new GridLayout();
            gridLayout.numColumns = 2;
            shell.setLayout(gridLayout);

            GridData data1 = new GridData();
            Label label1 = new Label(shell, SWT.PUSH);
            label1.setText("Label1");
            label1.setLayoutData(data1);

            Text text1 = new Text(shell, SWT.BORDER);
            text1.setText("");

            Label label2 = new Label(shell, SWT.PUSH);
            label2.setText("Label 222222222222222222222222222222");
            /*Text text2 = new Text(shell, SWT.BORDER);
            text2.setText("B4");*/

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

            while (!shell.isDisposed()) {
                if (!display.readAndDispatch())
                    display.sleep();
            }
        }
    }
Was it helpful?

Solution

Yes you can, add this line:

label2.setLayoutData(new GridData(SWT.BEGINNING, SWT.TOP, true, true, 2, 1));

It will make the second Label stretch across two columns of the GridLayout.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top