Reputation: 1391
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?
Upvotes: 2
Views: 5993
Reputation: 13984
Would I need to add a Gridlayout to createTopButtons?
No you need to put your buttons in another composite and this child composite should have a gridlayout with two columns.
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
Sure, why not. But you will also need something called GridData
and set it like area.setLayoutData(gridData);
. For centering something, your griddata may look like this gridData = new GridData(SWT.CENTER, SWT.CENTER, true, false);
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?
Yes. Though I dint get the second half composite created by the methods
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
public class SideBySide {
public static void main(String[] args) {
new SideBySide().start();
}
private Shell shell;
public void start()
{
Display display = new Display();
shell = new Shell(display);
shell.setLayout(new GridLayout(1, true));
GridData gridData = new GridData(SWT.FILL, SWT.FILL, true, true);
shell.setLayoutData(gridData);
shell.setText("Side By Side");
createDialogArea(shell);
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
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);
GridData gridData = new GridData(SWT.FILL, SWT.FILL, true, true);
shell.setLayoutData(gridData);
area.setLayoutData(gridData);
createTopButtons(area);
createTableViewer(area);
return area;
}
private void createTableViewer(Composite area)
{
Table table = new Table(area, SWT.BORDER|SWT.V_SCROLL|SWT.FULL_SELECTION);
GridData gridData = new GridData(SWT.FILL, SWT.FILL, true, true);
table.setLayoutData(gridData);
table.setLinesVisible(true);
table.setHeaderVisible(true);
TableColumn column = new TableColumn(table, SWT.LEFT);
column.setWidth(320);
column.setText("Column 1");
column = new TableColumn(table, SWT.LEFT);
column.setWidth(320);
column.setText("Column 2");
}
protected void createTopButtons(Composite parent)
{
Composite composite = new Composite(parent, SWT.NONE);
GridLayout gridLayout = new GridLayout(2, false);
gridLayout.marginWidth = 0;
gridLayout.marginHeight = 0;
gridLayout.verticalSpacing = 0;
gridLayout.horizontalSpacing = 0;
composite.setLayout(gridLayout);
GridData gridData = new GridData(SWT.FILL, SWT.FILL, true, false);
composite.setLayoutData(gridData);
gridData = new GridData(SWT.DEFAULT, SWT.FILL, false, false);
Button pdfButton = new Button(composite, SWT.PUSH);
pdfButton.setText("Create PDF");
pdfButton.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
shell.close();
}
});
pdfButton.setLayoutData(gridData);
Button plotButton = new Button(composite, SWT.PUSH);
plotButton.setText("Plot");
plotButton.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
shell.close();
}
});
plotButton.setLayoutData(gridData);
}
}
This is a great article to understand layouts in SWT.
Upvotes: 6