Time for action – using layouts

Now that ClockWidget has been created, multiple instances can be added into ClockView.

  1. Modify the 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);
  2. Run the test Eclipse instance and show the Clock View. Three clocks will be shown, counting in seconds:
    Time for action – using layouts
  3. In the 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);
  4. Run the code again now and the clocks will be in a row (horizontal):
    Time for action – using layouts
  5. Resize the view, the clocks will flow into different rows:
    Time for action – using layouts

    Note

    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 up
    • justify – if the components should be spaced so they reach the end
    • pack – if components should get their preferred size or expanded to fill space
    • wrap – if the components should wrap at the end of the line

    There 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).

  6. Every SWT widget has an optional layout data object, which is specific to the kind of layout being used by its containing parent. In the 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));
  7. Open the Clock View, and the clocks are shown in increasing order of size:
    Time for action – using layouts

What just happened?

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.

Pop quiz – understanding views

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?

Have a go hero – drawing hours and minute hands

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.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset