I would like to know what is the normal/common practices here for a SWT Composite object.
I have found out that whenever I add a Composite (with any UIs example: TextBox or Button) The UI created within the Composite does not align to the very starting edge of the Composite. (You can observe this by setting the background color of the Composite)
There is some space/padding within the Composite before the TextBox UI. This causes a misalignment in the GUI form I'm creating, if the previous UI is not created within a Composite.
I want to know what is the common practices to make them align? By setting some negative padding to move the Composite back so that the UI within it looks like it is aligned?
Sample code below!
public static void main(String[] args) {
Display display = new Display();
Shell shell = new Shell(display);
GridLayout layout = new GridLayout();
layout.numColumns = 1;
layout.makeColumnsEqualWidth = false;
shell.setLayout(layout);
Text t1 = new Text(shell, SWT.SINGLE | SWT.BORDER);
t1.setText("Test box...");
Composite c = new Composite(shell, SWT.NONE);
// c.setBackground(new Color(shell.getDisplay(), 255,0,0));
layout = new GridLayout();
layout.numColumns = 2;
layout.makeColumnsEqualWidth = true;
c.setLayout(layout);
Text t2 = new Text(c, SWT.SINGLE | SWT.BORDER);
t2.setText("Test box within Composite... not aligned to the first textbox");
Button b = new Button(c, SWT.PUSH);
b.setText("Button 1");
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
display.dispose();
}
This will fix it:
public static void main(String[] args) {
Display display = new Display();
Shell shell = new Shell(display);
shell.setLayout(new GridLayout(1, false));
Text t1 = new Text(shell, SWT.SINGLE | SWT.BORDER);
t1.setText("Test box...");
Composite c = new Composite(shell, SWT.NONE);
GridLayout layout = new GridLayout(2, true);
layout.marginWidth = 0; // <-- HERE
c.setLayout(layout);
Text t2 = new Text(c, SWT.SINGLE | SWT.BORDER);
t2.setText("Test box within Composite... not aligned to the first textbox");
Button b = new Button(c, SWT.PUSH);
b.setText("Button 1");
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
display.dispose();
}
Just set the marginWidth
if the GridLayout
to 0
.
Just a hint: You can set the number of columns and the equal-width thing in the constructor of GridLayout
.