Time for action – creating a reusable widget

Although the ClockView shows a single animated clock, creating an independent widget will allow the clock to be reused in other places.

  1. Create a new class in the com.packtpub.e4.clock.ui package, called ClockWidget, that extends Canvas.
  2. Create a constructor that takes a Composite parent and an int style bits parameter, and pass them to the superclass:
    public ClockWidget(Composite parent, int style) {
      super(parent, style);
    }
  3. Move the implementation of the drawClock method from the ClockView to the ClockWidget. Remove the PaintListener references from the ClockView class.
  4. In the ClockWidget constructor, register a PaintListener that delegates the call to the drawClock method:
    addPaintListener(this::drawClock); ...

Get Eclipse Plug-in Development Beginner's Guide - Second Edition 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.