I am trying to make a GUI for chatting and I placed three SWT controls in a row.
Unfortunately I can't find beautiful alignment for them.
The following code
Display display = new Display();
Shell shell = new Shell(display);
shell.setLayout(new GridLayout(3, false));
Label firstLabel = new Label(shell, SWT.NONE);
firstLabel.setText("PROMPT:");
firstLabel.setLayoutData(new GridData(SWT.BEGINNING, SWT.CENTER, false, false));
Text firstText = new Text(shell, SWT.NONE);
firstText.setText("hello");
firstText.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
Button firstButton = new Button(shell, SWT.PUSH);
firstButton.setText("Say");
firstButton.setLayoutData(new GridData(SWT.BEGINNING, SWT.CENTER, false, false));
shell.pack();
shell.open();
shell.setSize(400, 300);
while (!shell.isDisposed())
{
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
gives
i.e. text field is as tall as a button but text is not centered vertically inside it.
If code the text following way
Text firstText = new Text(shell, SWT.NONE);
firstText.setText("hello");
firstText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
I will get
The most beautiful result will be given by
Text firstText = new Text(shell, SWT.BORDER);
firstText.setText("hello");
firstText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
which is
but a button is again slightly taller than a text.
UPDATE
People say they have good result on non-windows platforms, but the question is probably how to control padding/margins of SWT controls.
UPDATE 2
Even in best case text field height is smaller that button's on Windows and the result is platform-dependent.
Here is a code example that should give you an idea how to solve your problem:
public static void main(String[] args)
{
Display display = new Display();
Shell shell = new Shell(display);
shell.setLayout(new GridLayout(3, false));
Label firstLabel = new Label(shell, SWT.NONE);
firstLabel.setText("PROMPT:");
firstLabel.setLayoutData(new GridData(SWT.BEGINNING, SWT.CENTER, false, false));
Text firstText = new Text(shell, SWT.BORDER);
firstText.setText("hello");
firstText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
Button firstButton = new Button(shell, SWT.PUSH);
firstButton.setText("Say");
firstButton.setLayoutData(new GridData(SWT.BEGINNING, SWT.CENTER, false, false));
Label secondLabel = new Label(shell, SWT.NONE);
secondLabel.setText("PROMPT:");
secondLabel.setLayoutData(new GridData(SWT.BEGINNING, SWT.CENTER, false, true));
Text secondText = new Text(shell, SWT.BORDER | SWT.MULTI | SWT.WRAP);
secondText.setText("hello");
secondText.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
Button secondButton = new Button(shell, SWT.PUSH);
secondButton.setText("Say");
secondButton.setLayoutData(new GridData(SWT.BEGINNING, SWT.CENTER, false, true));
shell.pack();
shell.open();
shell.setSize(400, 300);
while (!shell.isDisposed())
{
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
Looks like this:
Without SWT.BORDER
:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With