Stop a repeating timer when a button is pressed : SWT Timer « SWT « Java Tutorial

Stop a repeating timer when a button is pressed
 * Copyright (c) 2000, 2004 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * Contributors:
 *     IBM Corporation - initial API and implementation

 * Display example snippet: stop a repeating timer when a button is pressed
 * For a list of all SWT example snippets see
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.RowData;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;

public class TimeStopWhenButtonPressing {

  public static void main(String[] args) {
    final Display display = new Display();
    final Color red = display.getSystemColor(SWT.COLOR_RED);
    final Color blue = display.getSystemColor(SWT.COLOR_BLUE);
    Shell shell = new Shell(display);
    shell.setLayout(new RowLayout());
    Button button = new Button(shell, SWT.PUSH);
    button.setText("Stop Timer");
    final Label label = new Label(shell, SWT.BORDER);
    final int time = 500;
    final Runnable timer = new Runnable() {
      public void run() {
        if (label.isDisposed())
        Color color = label.getBackground().equals(red) ? blue : red;
        display.timerExec(time, this);
    display.timerExec(time, timer);
    button.addListener(SWT.Selection, new Listener() {
      public void handleEvent(Event event) {
        display.timerExec(-1, timer);
    label.setLayoutData(new RowData(button.getSize()));
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch())

17.125.SWT Timer
17.125.1.Stop a repeating timer when a button is pressedStop a repeating timer when a button is pressed
17.125.2.Create one repeating timer
17.125.3.Create two one shot timers (2000 ms)