Now that ClockWidget
has been created, multiple instances can be added into ClockView
.
ClockView
class's createPartControl()
method to create three ClockWidget
instances:final ClockWidget clock1 = new ClockWidget(parent, SWT.NONE); final ClockWidget clock2 = new ClockWidget(parent, SWT.NONE); final ClockWidget clock3 = new ClockWidget(parent, SWT.NONE);
ClockView
constructor, create a new RowLayout
with SWT.HORIZONTAL
, and then set it as the layout on parent Composite
:public void createPartControl(Composite parent) { RowLayout layout = new RowLayout(SWT.HORIZONTAL); parent.setLayout(layout);
RowLayout
has a number of fields that can affect how the widgets are laid out:
center
– if components are centered (vertically or horizontally)fill
– if the entire size of the parent should be taken upjustify
– if the components should be spaced so they reach the endpack
– if components should get their preferred size or expanded to fill spacewrap
– if the components should wrap at the end of the lineThere are also options to control any pixel spacing between elements (spacing
) and any margins at the edge (marginHeight
and marginWidth
, or which can be specified individually as marginTop
, marginBottom
, marginLeft
, and marginRight
).
ClockView
class's createPartControl()
method, add a RowData
object to the first and last clocks:clock1.setLayoutData(new RowData(20,20)); clock3.setLayoutData(new RowData(100,100));
A Composite
is capable of handling multiple widgets, and the job of deciding where to put these components is done by the associated LayoutManager
. The standard layout managers include FillLayout
, RowLayout
, GridLayout
, FormLayout
, and CellLayout
(note that CellLayout
is technically not SWT, but part of the Eclipse UI Workbench). The default for Eclipse Views is to use a FillLayout
; though a manually created Composite
has no associated layout by default.
Both FillLayout
and RowLayout
create a horizontal or vertical set of widgets with controlled sizes. FillLayout
is the default for views and expands the size of the widgets to the space available. RowLayout
will set the component's sizes to their default size as calculated by computeSize(0,0)
.
Layout managers have different properties such as SWT.HORIZONTAL
and SWT.VERTICAL
, which change how elements are wrapped if the row becomes full. The documentation for each layout manager has information as to what it supports.
Layout data objects are used to specify different values for objects within Composite
. The preceding example looked at RowData
options.
The corresponding FillData
class for FillLayout
has no public fields, and therefore is of lesser use. Other layout managers, such as GridLayout
, have more extensive customization options in the GridData
class. Remember when changing LayoutManager
the associated layout data objects will need to be modified accordingly.
Q1. What is the parent class of any views that you create?
Q2. How do you register views with the Eclipse workbench?
Q3. What two arguments are passed into every SWT widget and what are they for?
Q4. What does it mean for a widget to be disposed?
Q5. How do you draw a circle on a Canvas
?
Q6. What listener do you have to register to execute drawing operations?
Q7. What happens if you try and update an SWT object from outside a UI thread?
Q8. How do you update SWT components from a different thread?
Q9. What value is SWT.DEFAULT
used for?
Q10. How do you specify a specific size for a widget in a RowLayout
?
Now that the Clock View is animating a second hand, do the same calculation for the hour and minute hands. Minutes will be calculated the same way as seconds; for hours, multiply the hours by 5 to map onto the same path.
Draw lines for every five minutes using the drawLine()
function. Some simple maths will be required to calculate the start and end points of the line.
Finally, draw the text lettering for the numbers in the right locations. The drawText()
method can be used to place a string at a particular place. Use this to print out the current time in the center of the clock, or print out the date.