Mega Code Archive

 
Categories / Java Tutorial / SWT
 

Attaching a Control to a Position in the Parent Composite

To attach the control to a position in the parent composite, you need to define the position of a side of the control using a percentage value. import org.eclipse.swt.SWT; import org.eclipse.swt.layout.FormAttachment; import org.eclipse.swt.layout.FormData; import org.eclipse.swt.layout.FormLayout; import org.eclipse.swt.widgets.Button; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Shell; public class FormLayoutAttachControl {   public static void main(String[] args) {     Display display = new Display();     final Shell shell = new Shell(display);     shell.setLayout(new FormLayout());     FormData formData = new FormData();     formData.left = new FormAttachment(50);     Button button1 = new Button(shell, SWT.PUSH);     button1.setText("button1");     button1.setLayoutData(formData);     shell.setSize(450, 400);     shell.open();     while (!shell.isDisposed()) {       if (!display.readAndDispatch()) {         display.sleep();       }     }     display.dispose();   } }