A GridLayout Example: This layout manager allows you to build complex and flexible UIs.|
GridLayout with GridData
GridLayout is the most important of the managers. Its features allow you to build complex and flexible UIsand it's especially useful for when you need dynamic behavior in your screens. The GridLayout lays out the widgets in grids. Its power lies in its ability to configure GridData for each control in the layout.
GridData is the layout data object associated with GridLayout. To set a GridData object into a control, you use the setLayoutData() method.
The Fill and Grab parameters control the dynamic behavior of the screen. The marginWidth and marginHeight parameters control the space in pixels to be left along the left, right, top, and bottom of the screen.
The HorizontalSpacing and VerticalSpacing values specify the amount of space left between the widgets in pixels. The numColumns value specifies the number of columns in the grid:
txtItem.setLayoutData( new GridData( GridData.HORIZONTAL_ALIGN_FILL |
lstItems.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_FILL |
In the above code, the textItem
, the GRAB_HORIZONTAL
parameter enables the widgets to get most of the horizontal space when the screen is expanded. Find the full source code here
The ListItem gets most of the vertical space as the GRAB_VERTICAL space is set to true. However unlike GridBagLayout of Swing, GridLayout does not support weighted fill for multiple components.
|Figure 3. A Blueprint Design: This shows a blueprint design for setting a GridData object into a control.|
FormLayout with FormData and FormAttachment
FormLayout is very flexible. It allows you to control the position and size of the components within the layout to the exact pixel.
Use FormData to define the attachments of a widget in a FormLayout. To attach each side of a child widget to a position in the parent composite, or to other widgets within the composite, create instances of FormAttachment and setting them into the top, bottom, left, and right fields of the child's FormData. Use FormAttachments to determine the size of the control. Use FormData objects to set the width and height of controls within a FormLayout. If you're not careful, and you make a circular attachment, the layout algorithm will fail, which can incur undesirable results.
This layout stacks all the controls one on top of the other and resizes all the controls to have the same size and locationhowever only one control is visible at a time. The control specified in topControl is visible. Users must set the topControl value to flip between the visible items and the call layout() on the composite which contains the StackLayout.
layout.topControl = bArray[index];
Though this layout has some similarities to Swing's TabbedPane
allows only sequential navigation of each control.
Using any combination of these layout managers, you can build complex, user-friendly screens for Java applications.