Positioning the Text Field
When
building a GUI, you need to have precise control over the positioning
of every widget that makes up the interface. In SWT, there are two
ways to specify the size and position of a widget within a container.
The first approach is to use the setBounds()
or
setLocation( )
methods that are part of the super
class for every widget.
How do I do that?
In the TextFieldExample
class, the
Text
widget is positioned with this:
text1.setBounds(10,10,100,20);
Placing a text field in a particular location on the window is simply a matter of adjusting the first two parameters, which represent the x and y coordinates of the upper lefthand corner of the text field with respect to the upper lefthand corner of the shell’s workspace area (the area below the title bar and menu).
Note
A toolbar that is displayed on the shell takes up room beginning in the upper left-hand corner of the workspace area, so you must account for the height of the toolbar in calculating where to place a widget.
The first setBounds( )
parameter moves the text
field’s position to the right as the
parameter’s value increases, while the second
parameter moves the field’s position closer to the
bottom of the window as the value increases. Aligning widgets along
their lefthand side is a matter of specifying the same value in the
first parameter passed to setBounds( )
. Example 5-2 demonstrates how to align two
Text
widgets along their lefthand sides, with the
result shown in Figure 5-3.
Example 5-2. Aligning ...
Get SWT: A Developer's Notebook now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.