The GridData class is the layout data associated with the GridLayout class.
- Horizontal Alignment and Vertical Alignment.
- Specify how a control is positioned horizontally and vertically in a cell.
- Several possible values for horizontalAlignment are as follows:
- BEGINNING: Left-aligned (the default value)
- CENTER: Center-aligned
- END: Right-aligned
- FILL: Filling all the available horizontal space
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.List;
import org.eclipse.swt.widgets.Shell;
public class GridLayoutAlignmentHoriVerical {
public static void main(String[] args) {
Display display = new Display();
final Shell shell = new Shell(display);
GridLayout gridLayout = new GridLayout();
gridLayout.numColumns = 2;
gridLayout.makeColumnsEqualWidth = true;
shell.setLayout(gridLayout);
Button button1 = new Button(shell, SWT.PUSH);
button1.setText("button1"); // Default alignment
List list = new List(shell, SWT.BORDER);
list.add("item 1");
list.add("item 2");
list.add("item 3");
list.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_CENTER));
Button button2 = new Button(shell, SWT.PUSH);
button2.setText("button #2");
button2.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_END));
Button button3 = new Button(shell, SWT.PUSH);
button3.setText("3");
button3.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_FILL));
shell.setSize(450, 400);
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
display.dispose();
}
}