GridLayout
provides much flexibility to design user interfaces. GridLayout provides number
of properties to manipulate to get good looking user interface.
How to define
GridLayout?
GridLayout
gridLayout = new GridLayout();
gridLayout.numColumns
= 2;
shell.setLayout(gridLayout);
Following
are the different properties associated with GridLayout instance.
Property
|
Description
|
horizontalSpacing
|
Specifies
the number of pixels between the right edge of one cell and the left edge of
its neighbouring cell to the right.
|
makeColumnsEqualWidth
|
makeColumnsEqualWidth
specifies whether all columns in the layout will be forced to have the same
width. Default value is false.
|
marginBottom
|
marginBottom
specifies the number of pixels of vertical margin that will be placed along
the bottom edge of the layout.
|
marginLeft
|
marginLeft
specifies the number of pixels of horizontal margin that will be placed along
the left edge of the layout.
|
marginRight
|
marginRight
specifies the number of pixels of horizontal margin that will be placed along
the right edge of the layout.
|
marginTop
|
marginTop
specifies the number of pixels of vertical margin that will be placed along
the top edge of the layout.
|
marginWidth
|
marginWidth
specifies the number of pixels of horizontal margin that will be placed along
the left and right edges of the layout.
|
numColumns
|
numColumns
specifies the number of cell columns in the layout.
|
verticalSpacing
|
verticalSpacing
specifies the number of pixels between the bottom edge of one cell and the
top edge of its neighbouring cell underneath.
|
What is GridData
objects?
GridData
object is the layout data object associated with GridLayout, this is used to
control the behavior of widgets.
Following
statements tells the layout to span the button across two columns.
GridData
data3 = new GridData();
data3.horizontalSpan
= 2;
button.setLayoutData(data3);
How to define
GridData object?
There
are two way.
Way1
Define
GridData object and set the properties.
Ex:
GridData
gridData = new GridData();
gridData.horizontalAlignment
= GridData.FILL;
gridData.verticalAlignment
= GridData.FILL;
gridData.grabExcessHorizontalSpace
= true;
gridData.grabExcessVerticalSpace
= true;
gridData.horizontalSpan
= 2;
button2.setLayoutData(gridData);
Way 2
Define
using parameterized constructors.
Ex:
GridData
gridData1 = new GridData (SWT.FILL, SWT.CENTER, true, false)
GridData
gridData2 = new GridData (SWT.FILL, SWT.FILL, true, true, 2, 1)
button1.setLayoutData(gridData1);
button1.setLayoutData(gridData2);
Note:
Don't
reuse the GridData object, As per JavaSpecification, Every control in a
Composite that is managed by a GridLayout must have a unique GridData object.
If the layout data for a control in a GridLayout is null at layout time, a
unique GridData object is created for it.
Following
is the complete working application.
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.Label; import org.eclipse.swt.widgets.Shell; import org.eclipse.swt.widgets.Text; public class Test { private static void addWidgetsToShell(Display display, Shell shell) { GridLayout gridLayout = new GridLayout(); gridLayout.numColumns = 2; shell.setLayout(gridLayout); Label label1 = new Label(shell, SWT.NONE); label1.setText("Name:"); Text text1 = new Text(shell, SWT.BORDER); Label label2 = new Label(shell, SWT.NONE); label2.setText("Age:"); Text text2 = new Text(shell, SWT.BORDER); Label label3 = new Label(shell, SWT.NONE); label3.setText("Gender:"); Text text3 = new Text(shell, SWT.BORDER); Button button = new Button(shell, 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); 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, 200); 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