FormLayout
is the advanced layout out of the four layouts (FillLayout, RowLayout,
GridLayout). It gives flexibility to control the position and size of the
children of a composite control by using FormAttachments.
Ex:
FormLayout
formLayout = new FormLayout();
formLayout.marginWidth
= 3;
formLayout.marginHeight
= 3;
shell.setLayout(formLayout);
Just
like how we used GridData instance to control widgets in GridLayout, there is
FormData instance we can use to control the widgets in FormLayout.
How to define
FormData object?
By
using FormData instance, we can attach the SWT widgets to FormLayout.
Ex:
Composite
leftComposite = new Composite(shell, SWT.BORDER);
FormData
formData = new FormData();
formData.top
= new FormAttachment(0, 10);
formData.left
= new FormAttachment(0, 10);
formData.bottom
= new FormAttachment(75, 0);
formData.right
= new FormAttachment(40, 0);
leftComposite.setLayoutData(formData);
What is
FormAttachment?
FormAttachment
instances are used to define edges of widget within a FormLayout. We can set
FormAttachments to top, bottom, left and right fields of FormData.
What are the
constants top, left, bottom, right?
Following
table summarizes the constants.
Constant
|
Description
|
top
|
top
specifies the attachment of the top of the control (widget).
|
bottom
|
bottom
specifies the attachment of the bottom of the control.
|
left
|
left
specifies the attachment of the left side of
the control.
|
right
|
right
specifies the attachment of the right side of the control.
|
FormAttachment
class provides following constructors.
Constructor
|
Description
|
public
FormAttachment ()
|
Constructs
a new instance of this class. The numerator is zero, the denominator is 100
and the offset is zero.
|
public
FormAttachment (int numerator)
|
Constructs
a new instance of this class with given numerator, denominator to 100, offset
to 0.
|
public
FormAttachment (int numerator, int offset)
|
Constructs
a new instance of this class with given numerator, offset and denominator of
100.
|
public
FormAttachment (int numerator, int denominator, int offset)
|
Constructs
a new instance of this class with given numerator, denominator , offset.
|
public
FormAttachment (Control control)
|
Constructs
a new instance of this class given a control. Since no alignment is
specified, the default alignment is to attach the side to the adjacent side
of the specified control. Since no offset is specified, an offset of 0 is
used.
|
public
FormAttachment (Control control, int offset)
|
Constructs
a new instance of this class given a control. Since no alignment is
specified, the default alignment is to attach the side to the adjacent side
of the specified control. Since no offset is specified, an offset of 0 is
used.
|
public
FormAttachment (Control control, int offset, int alignment)
|
Constructs
a new instance of this class given a control, an offset and an alignment.
Following
are the possible alignments.
SWT.TOP
: The side will be attached to the top side of the specified control
SWT.BOTTOM
: The side will be attached to the bottom side of the specified control
SWT.LEFT
: The side will be attached to the left side of the specified control
SWT.CENTER
: The side will be attached to the right side of the specified control
SWT.DEFAULT
: The side will be attached to the adjacent side of the specified control
|
What are the
numerator and denominators?
A
FormAttachment defines where to attach the side of a control by using the
equation, y = ax + b. The "a" term represents a fraction of the
parent composite's width (from the left) or height (from the top). It can be
defined using a numerator and denominator, or just a percentage value. If a
percentage is used, the denominator is set to 100. The "b" term in
the equation represents an offset, in pixels, from the attachment position
Following
is the complete working application.
import org.eclipse.swt.SWT; import org.eclipse.swt.layout.FillLayout; import org.eclipse.swt.layout.FormAttachment; import org.eclipse.swt.layout.FormData; import org.eclipse.swt.layout.FormLayout; import org.eclipse.swt.layout.GridData; import org.eclipse.swt.layout.GridLayout; import org.eclipse.swt.layout.RowLayout; import org.eclipse.swt.widgets.Button; import org.eclipse.swt.widgets.Composite; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Label; import org.eclipse.swt.widgets.Shell; import org.eclipse.swt.widgets.Text; public class Test { private static Composite addLeftComposite(Shell shell) { /* Add Left composite to shell */ Composite leftComposite = new Composite(shell, SWT.BORDER); FillLayout fillLayout = new FillLayout(); fillLayout.type = SWT.VERTICAL; leftComposite.setLayout(fillLayout); Label label0 = new Label(leftComposite, SWT.NONE); label0.setText("File"); Label label1 = new Label(leftComposite, SWT.NONE); label1.setText("Edit"); Label label2 = new Label(leftComposite, SWT.NONE); label2.setText("Source"); Label label3 = new Label(leftComposite, SWT.NONE); label3.setText("Refactor"); Label label4 = new Label(leftComposite, SWT.NONE); label4.setText("Navigate"); Label label5 = new Label(leftComposite, SWT.NONE); label5.setText("Search"); Label label6 = new Label(leftComposite, SWT.NONE); label6.setText("Project"); Label label7 = new Label(leftComposite, SWT.NONE); label7.setText("Run"); Label label8 = new Label(leftComposite, SWT.NONE); label8.setText("Window"); Label label9 = new Label(leftComposite, SWT.NONE); label9.setText("Help"); FormData formData = new FormData(); formData.top = new FormAttachment(0, 10); formData.left = new FormAttachment(0, 10); formData.bottom = new FormAttachment(75, 0); formData.right = new FormAttachment(40, 0); leftComposite.setLayoutData(formData); return leftComposite; } private static Composite addBottomComposite(Shell shell, Composite leftPanel) { RowLayout rowLayout = new RowLayout(); Composite bottomComposite = new Composite(shell, SWT.NONE); bottomComposite.setLayout(rowLayout); Button b1 = new Button(bottomComposite, SWT.PUSH); b1.setText("File"); Button b2 = new Button(bottomComposite, SWT.PUSH); b2.setText("Edit"); Button b3 = new Button(bottomComposite, SWT.PUSH); b3.setText("Source"); Button b4 = new Button(bottomComposite, SWT.PUSH); b4.setText("Refactor"); Button b5 = new Button(bottomComposite, SWT.PUSH); b5.setText("Navigate"); Button b6 = new Button(bottomComposite, SWT.PUSH); b6.setText("Search"); Button b7 = new Button(bottomComposite, SWT.PUSH); b7.setText("Search"); Button b8 = new Button(bottomComposite, SWT.PUSH); b8.setText("Search"); Button b9 = new Button(bottomComposite, SWT.PUSH); b9.setText("Search"); Button b10 = new Button(bottomComposite, SWT.PUSH); b10.setText("Search"); FormData formRow = new FormData(); formRow.top = new FormAttachment(leftPanel, 10); formRow.left = new FormAttachment(0, 10); formRow.bottom = new FormAttachment(100, -10); formRow.right = new FormAttachment(100, -10); bottomComposite.setLayoutData(formRow); return bottomComposite; } private static Composite addRightComposite(Shell shell, Composite leftPanel) { Composite rightComposite = new Composite(shell, SWT.NONE); GridLayout gridLayout = new GridLayout(); gridLayout.numColumns = 2; rightComposite.setLayout(gridLayout); Label label1 = new Label(rightComposite, SWT.NONE); label1.setText("Name:"); Text text1 = new Text(rightComposite, SWT.BORDER); Label label2 = new Label(rightComposite, SWT.NONE); label2.setText("Age:"); Text text2 = new Text(rightComposite, SWT.BORDER); Label label3 = new Label(rightComposite, SWT.NONE); label3.setText("Gender:"); Text text3 = new Text(rightComposite, SWT.BORDER); Button button = new Button(rightComposite, SWT.CHECK); button.setText("Have you been employed in the past six months?"); GridData gridData1 = new GridData(); GridData gridData2 = new GridData(); GridData gridData3 = new GridData(); gridData1.widthHint = 60; gridData2.widthHint = 60; gridData3.widthHint = 60; label1.setLayoutData(gridData1); label2.setLayoutData(gridData2); label3.setLayoutData(gridData3); GridData gridData4 = new GridData(GridData.FILL_HORIZONTAL); GridData gridData5 = new GridData(GridData.FILL_HORIZONTAL); GridData gridData6 = new GridData(GridData.FILL_HORIZONTAL); text1.setLayoutData(gridData4); text2.setLayoutData(gridData5); text3.setLayoutData(gridData6); GridData gridData7 = new GridData(); gridData7.horizontalSpan = 2; button.setLayoutData(gridData7); FormData formGrid = new FormData(); formGrid.top = new FormAttachment(0, 10); formGrid.left = new FormAttachment(leftPanel, 10); formGrid.right = new FormAttachment(100, -10); formGrid.bottom = new FormAttachment(leftPanel, 10, SWT.BOTTOM); rightComposite.setLayoutData(formGrid); return rightComposite; } private static void addWidgetsToShell(Display display, Shell shell) { FormLayout formLayout = new FormLayout(); formLayout.marginWidth = 3; formLayout.marginHeight = 3; shell.setLayout(formLayout); Composite leftComposite = addLeftComposite(shell); addBottomComposite(shell, leftComposite); addRightComposite(shell, leftComposite); shell.open(); } public static void main(String[] args) { /* Instantiate Display object, it represents SWT session */ Display display = new Display(); /* * Define Shell, it represent a window, You can add more than one shell * to Display */ Shell shell = new Shell(display); shell.setSize(700, 500); shell.setText("GridLayout Demo"); addWidgetsToShell(display, shell); /* * Run the event dispatching loop until an exit condition occurs, which * is typically when the main shell window is closed by the user. */ while (!shell.isDisposed()) { if (!display.readAndDispatch()) { display.sleep(); } } /* Dispose the display */ display.dispose(); } }
No comments:
Post a Comment