You are on page 1of 15

CHPT2.

LAYOUT ADN
COMPOSITE
1. LAYOUT
When writing applications in SWT, you may need to use layouts to give your
windows a specific look. A layout controls the position and size of children in a
Composite. Layout classes are subclasses of the abstract class Layout. SWT
provides several standard layout classes, and you can write custom layout classes.
The standard layout classes in the SWT library are:
· FillLayout – lays out equal-sized widgets in a single row or column.
· RowLayout – lays out widgets in a row or rows, with fill, wrap, and spacing
options.
· GridLayout – lays out widgets in a grid.
· FormLayout – lays out widgets by creating attachments for each of their sides.
· ......
StackLayout – only displays the topmost controls.
1.1 FILLLAYOUT
FillLayout is the simplest layout class. It fills the entire container with components
in a row or column and enforces a consistent size of components. Typically, the
widgets will all be as tall as the tallest widget, and as wide as the widest.
FillLayout does not wrap, and you cannot specify margins or spacing. You might
use it to lay out buttons in a task bar or tool bar, or to stack checkboxes in a Group.

No parameter: FillLayout fillLayout = new FillLayout();

With parameter: FillLayout fillLayout = new FillLayout(int type);

Here's the code


1.2 ROWLAYOUT
RowLayout is more commonly used than FillLayout because of its ability to
wrap, and it provides configurable margins and spacing. RowLayout has a
number of configuration fields. In addition, the height and width of each
widget in a RowLayout can be specified by setting a RowData object into the
widget using setLayoutData.
RowLayout rowlayout = new RowLayout(int type);
eg: SWT.VERTICAL, SWT.HORIZONTAL

boolean justify: Whether the distance of components is stretched


boolean wrap: Whether to automatically wrap
boolean pack: Whether the components have the same size
Here's the code
1.3 GRIDLAYOUT
GridLayout is the most useful and powerful of the standard layouts, but it is also the
most complicated. With a GridLayout, the widget children of a Composite are laid
out in a grid. GridLayout has a number of configuration fields, and, like
RowLayout, the widgets it lays out can have an associated layout data object, called
GridData. The power of GridLayout lies in the ability to configure GridData for
each widget controlled by the GridLayout.

GridLayout gridlayout = new GridLayout(int numColumns, true);

gridlayout.horizontalSpacing: set gap between columns

Here's the code


1.4 FORMLAYOUT
FormLayout is a very flexible and precise layout, which is newly added in
SWT2.0. FormLayout has a dedicated layout data class FormData, in addition,
a FormAttachment class has been added.
It lays out components by creating the distance of each side of the component,
and uses marginWidth, marginHeight to set the margin.
FormLayout formlayout = new
FormLayout();
formlayout.marginTop: int;
formlayout.marginLeft: int;
......

Here's the code


1.5 STACKLAYOUT
StackLayout stacks all the controls one on top of the other and resizes all
controls to have the same size and location. The control specified in
topControl is visibleand all other controls are not visible.

StackLayout layout = new StackLayout();


Before: After click:

Click: add a new text

Here's the code


2. COMPOSITE
We can perform unified operations on components through the container.
When the container moves, the components in it will also move. If the
container is hidden, the components will also be hidden. When the container is
destroyed, the components will also be automatically destroyed.

The standard composition classes in the SWT library are:


· Composite · Group
· ScrolledComposite · SashForm
· TabFolder · CBanner
......
2.1 COMPOSITE
Composite container is the most basic container type in SWT, and it is the
parent class of other container types. In Composite, Radio Buttons are
exclusive, only one of them can be selected, setting the style
SWT.NO_RADIO_GROUP can change this behavior.

Composite composite = new Composite(shell, SWT.NONE);


composite.setBounds(int, int, int, int);
no border: SWT.NONE
with border: SWT.BORDER

Here's the code


2.2 GROUP
Group container is a subclass of Composite, so they are basically the same.
The main difference is that group container provides a default border and
supports displaying a line of text title at the top left of the border.

Group group = new Group(shell, SWT.NONE);


group.setBounds(10, 10, 202, 80);
group.setText("This a Group");

Here's the code


2.3 SCROLLEDCOMPOSITE
ScrolledComposite is a subclass of Composite, It can contain other controls
within a scrolling frame.

c1 = new ScrolledComposite(shell,
SWT.BORDER);

expanding button:
c1.setExpandHorizontal(true);
c1.setExpandVertical(true);

Here's the code


2.4 SASHFORM
The SashForm lays out its children in a Row or Column arrangement (as
specified by the orientation) and places a Sash between the children. It can divide
the area of the screen into several parts, and the window frame can be dragged to
resize the window.

SashForm form = new SashForm(shell,SWT.HORIZONTAL);


form.setWeights(new int[] {30,40,30});

Here's the code


2.5 TABFOLDER
TabFolder can have one or more TabItems, and TabItems can set the controls to be
displayed through the setControl method, which can be basic controls or containers. Tab
positions can also be created at the bottom, for example:
TabFolder tab= new TabFolder(shell,SWT.BOTTOM);

The use method of CTabFloder is similar with TabFolder, and the function is more powerful, for
example:
With close button:
CTabFolder cf=new CTabFolder(shell,SWT.CLOSE);
With border:
new CTabFolder(shell,SWT.BORDER);
Here's the code
2.6 CBANNER
CBanner is used in the workbench to layout the toolbar area and perspective
switching toolbar. It draws a separator between the left and right children which
can be dragged to resize the right control.

CBanner banner=new CBanner(shell,SWT.BORDER);


banner.setLeft(...);
banner.setRight(...);
banner.setBottom(...);
Change the shape of dividing lines: banne.setSimple(false);

Here's the code

You might also like