I have a method to create 2 buttons. These buttons are different than the OK, Close buttons. They will perform different actions when clicked. I want the 2 buttons side by side and at the top of my base composite. Which is using GridLayout. I want to be able to place the buttons side by side.
Here is my createDialogArea that I am adding the method to.
protected Control createDialogArea(Composite parent) {
final Composite area = new Composite(parent, SWT.NONE);
final GridLayout gridLayout = new GridLayout();
gridLayout.marginWidth = 15;
gridLayout.marginHeight = 10;
area.setLayout(gridLayout);
createTopButtons(area);
createTableViewer(area);
return area;
}
Here is the button method.
protected void createTopButtons(Composite parent) {
Button pdfButton = new Button(parent, SWT.PUSH);
pdfButton.setText("Create PDF");
pdfButton.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
close();
}
});
Button plotButton = new Button(parent, SWT.PUSH);
plotButton.setText("Plot");
plotButton.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
close();
}
});
}
Would I need to add a Gridlayout to createTopButtons?
If so how would I get them side by side and not one on top of the other.
Also in the createDialogArea, can I arrange my components using the gridLayout?
Example – I want createTopButtons to be on the top left hand side, then I want createTableViewer centered
Do you arrange your items/buttons/labels using a layout inside the composite you are creating like createTopButtons. Then use a layout in the createDialogArea to arrange the composite created by the methods?
Q & A
No you need to put your buttons in another composite and this child composite should have a gridlayout with two columns.
Sure, why not. But you will also need something called
GridDataand set it likearea.setLayoutData(gridData);. For centering something, your griddata may look like thisgridData = new GridData(SWT.CENTER, SWT.CENTER, true, false);Yes. Though I dint get the second half
composite created by the methodsCode
Code Output
Further Reading
This is a great article to understand layouts in SWT.