blob: 0cd720e1e1d35b59ecb983fcc90ceafee61b6df0 [file] [log] [blame]
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"><HTML>
<HEAD>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=ISO-8859-1">
<META HTTP-EQUIV="Content-Style-Type" CONTENT="text/css">
<LINK REL="STYLESHEET" HREF="../book.css" CHARSET="ISO-8859-1" TYPE="text/css">
<TITLE>
Layouts
</TITLE>
<link rel="stylesheet" type="text/css" HREF="../book.css">
</HEAD>
<BODY BGCOLOR="#ffffff">
<H2>
Layouts</H2>
<P >
We have seen some simple examples that show how to size or position child widgets based on the size of the parent. So far,
this kind of computation has occurred in response to a resize listener. This is often the best way to handle simple widget positioning. However, there are common patterns used by applications when placing widgets. These patterns can be structured as configurable layout algorithms that can be reused by many different applications.</P>
<P >
SWT defines <em> layouts</em> that provide general purpose positioning and sizing of child widgets in a composite. Layouts are subclasses of the abstract class
<strong><a href="../reference/api/org/eclipse/swt/widgets/Layout.html">Layout</a></strong>. The SWT standard layouts can be found in the
<strong><a href="../reference/api/org/eclipse/swt/layout/package-summary.html"> org.eclipse.swt.layout</a></strong> package.</P>
<P >
You should understand some general definitions when resizing and positioning
widgets: </P>
<ul>
<li>The <em>location</em> of a widget is its x,y coordinate location within its
parent widget.</li>
<li>The <em>preferred size</em> of a widget is the minimum size needed to show
its content. This is computed differently for each kind of widget.&nbsp; In
the case of a composite, the preferred size is the minimum size that
contains the composite and all of its children at their preferred sizes.</li>
<li>The <em>clientArea</em> is the size of a widget's content area.</li>
<li>The <em>trim</em> is the distance between a widget's client area and its
actual border.&nbsp; Trim is occupied by the widget's borders or extra space
at its edge.&nbsp; The size and appearance of the trim is widget
and platform dependent.</li>
</ul>
<P >
These concepts are relevant for applications regardless of whether a layout is used. You can think of a layout as a convenient way to package resize functionality for reuse. </P>
<P >
Some additional concepts are introduced by layouts: </P>
<ul>
<li>Some layouts support&nbsp; <em>spacing</em> between widgets in the
layout.&nbsp;&nbsp;</li>
<li>Some layouts support a <em>margin</em> between the edge of the layout and
the widget adjacent to the edge.</li>
</ul>
<P >
See <a href="http://www.eclipse.org/articles/Understanding%20Layouts/Understanding%20Layouts.htm">Understanding
layouts in SWT</a>&nbsp; for further discussion and pictures demonstrating these concepts. </P>
<P >
The following code snippet shows the simple case of an application using a resize callback to size a label to the size of its parent shell:</P>
<font color='#4444CC'><pre>
Display display = new Display ();
Shell shell = new Shell (display);
Label label = new Label (shell, SWT.CENTER);
shell.addControlListener (new ControlAdapter () {
public void controlResized (ControlEvent e) {
label.setBounds (shell.getClientArea ());
}
});
</pre></font>
<P >
The next snippet uses a layout to achieve the same effect:</P>
<font color='#4444CC'><pre>
Display display = new Display ();
Shell shell = new Shell (display);
Label label = new Label (shell, SWT.CENTER);
shell.setLayout (new FillLayout ());
</pre></font>
<P >
Even for this simple example, using a layout reduces the application code. For more complex layouts, the simplification is much greater.</P>
<P >
SWT provides four default layout classes that can be used for many situations.</P>
<p><a href="../hglegal.htm"><img border="0" src="../ngibmcpy.gif" alt="Copyright IBM Corporation and others 2000, 2003." border="0" width="324" height="14"></a></p>
</BODY>
</HTML>