How to fill text fields 100% horizontally

If I have a text field with SWT, how can I get a field to fill up to 100% or a certain width.

For example, this text box will only be available horizontally.

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

        Button button0 = new Button(shell, SWT.PUSH);
        button0.setText ("button0");

        Text text = new Text(shell, SWT.BORDER | SWT.FILL);
        text.setText ("Text Field");

        shell.setSize(500, 400);
        //shell.pack();
        shell.open();

        while (!shell.isDisposed ()) {
            if (!display.readAndDispatch ())
                display.sleep ();
        }
        display.dispose ();
    }
}
+5
source share
2 answers

Do something like this:

Text text = new Text(shell, SWT.BORDER);
text.setText ("Text Field");
text.setLayoutData(new GridData(SWT.FILL, SWT.CENTER));

/: Since this is an accepted answer, I delete the errors. thanks for correcting me.

+4
source

. GridLayout. , LayoutData, , , . GridLayout GridData.

, , GridData, :

// Fills available horizontal and vertical space, grabs horizontal space,grab
// does not  grab vertical space
GridData gd = new GridData(SWT.FILL, SWT.FILL, true, false);
text.setLayoutData(gd);

LayoutManager, FormLayout. FormData, , .

, , .

new GridData (int style) " " . , .

+4

All Articles