To add an option for
ClockWidget
to have a different color, an instance must be obtained, instead of the hardcoded BLUE
reference. Since Color
objects are Resources
, they must be disposed correctly when the widget is disposed.
To avoid passing in a Color
directly, the constructor will be changed to take an RGB
value (which is three int
values), and use that to instantiate a Color
object to store for later. The lifetime of the Color
instance can be tied to the lifetime of ClockWidget
.
Add a
private final Color
instance calledcolor
toClockWidget
:private final Color color;
Modify the constructor of
ClockWidget
to take anRGB
instance, and use it to instantiate aColor
object. Note that the color is leaked at this point, and will be fixed later:public ClockWidget(Composite parent, int style, RGB rgb) { super(parent, style); // FIXME color is leaked! this.color = new Color(parent.getDisplay(),rgb); ...