Example usage for org.eclipse.swt.widgets Display timerExec

List of usage examples for org.eclipse.swt.widgets Display timerExec

Introduction

In this page you can find the example usage for org.eclipse.swt.widgets Display timerExec.

Prototype

public void timerExec(int milliseconds, Runnable runnable) 

Source Link

Document

Causes the run() method of the runnable to be invoked by the user-interface thread after the specified number of milliseconds have elapsed.

Usage

From source file:TimerOneShot2000.java

public static void main(String[] args) {
    Display display = new Display();
    Shell shell = new Shell(display);
    shell.setSize(200, 200);/*  w ww . ja v  a  2 s .  c om*/
    shell.open();
    display.timerExec(2000, new Runnable() {
        public void run() {
            System.out.println("2000");
        }
    });

    while (!shell.isDisposed()) {
        if (!display.readAndDispatch())
            display.sleep();
    }
    display.dispose();
}

From source file:org.eclipse.swt.snippets.Snippet60.java

public static void main(String[] args) {
    Display display = new Display();
    Shell shell = new Shell(display);
    shell.setText("Snippet 60");
    shell.setSize(200, 200);//from   www .j  av a2s. c om
    shell.open();
    display.timerExec(5000, () -> System.out.println("5000"));
    display.timerExec(2000, () -> System.out.println("2000"));
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch())
            display.sleep();
    }
    display.dispose();
}

From source file:org.eclipse.swt.snippets.Snippet57.java

public static void main(String[] args) {
    final Display display = new Display();
    Shell shell = new Shell(display);
    shell.setText("Snippet 57");
    final ProgressBar bar = new ProgressBar(shell, SWT.SMOOTH);
    Rectangle clientArea = shell.getClientArea();
    bar.setBounds(clientArea.x, clientArea.y, 200, 32);
    shell.open();/*w ww . ja v a 2s.  c  o m*/

    display.timerExec(100, new Runnable() {
        int i = 0;

        @Override
        public void run() {
            if (bar.isDisposed())
                return;
            bar.setSelection(i++);
            if (i <= bar.getMaximum())
                display.timerExec(100, this);
        }
    });

    while (!shell.isDisposed()) {
        if (!display.readAndDispatch())
            display.sleep();
    }
    display.dispose();
}

From source file:TimerRepeating.java

public static void main(String[] args) {
    final Display display = new Display();
    final Shell shell = new Shell(display);
    final int time = 500;

    Runnable timer = new Runnable() {
        public void run() {
            Point point = display.getCursorLocation();
            Rectangle rect = shell.getBounds();
            if (rect.contains(point)) {
                System.out.println("In");
            } else {
                System.out.println("Out");
            }//from w w w.  j a va  2s  .  co  m
            display.timerExec(time, this);
        }
    };
    display.timerExec(time, timer);

    shell.setSize(200, 200);
    shell.open();
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch())
            display.sleep();
    }
    display.dispose();
}

From source file:org.eclipse.swt.snippets.Snippet16.java

public static void main(String[] args) {
    final Display display = new Display();
    final Shell shell = new Shell(display);
    shell.setText("Snippet 16");
    final int time = 500;
    Runnable timer = new Runnable() {
        @Override//  www  . j av a  2  s .c  o m
        public void run() {
            if (shell.isDisposed())
                return;
            Point point = display.getCursorLocation();
            Rectangle rect = shell.getBounds();
            if (rect.contains(point)) {
                System.out.println("In");
            } else {
                System.out.println("Out");
            }
            display.timerExec(time, this);
        }
    };
    display.timerExec(time, timer);
    shell.setSize(200, 200);
    shell.open();
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch())
            display.sleep();
    }
    display.dispose();
}

From source file:org.eclipse.swt.snippets.Snippet275.java

public static void main(String[] args) {
    final int INTERVAL = 888;
    final Display display = new Display();
    final Image image = new Image(display, 750, 750);
    GC gc = new GC(image);
    gc.setBackground(display.getSystemColor(SWT.COLOR_RED));
    gc.fillRectangle(image.getBounds());
    gc.dispose();/*  w ww. j a va  2 s  .  c  o  m*/

    Shell shell = new Shell(display);
    shell.setText("Snippet 275");
    shell.setBounds(10, 10, 790, 790);
    final Canvas canvas = new Canvas(shell, SWT.NONE);
    canvas.setBounds(10, 10, 750, 750);
    canvas.addListener(SWT.Paint, event -> {
        value = String.valueOf(System.currentTimeMillis());
        event.gc.drawImage(image, 0, 0);
        event.gc.drawString(value, 10, 10, true);
    });
    display.timerExec(INTERVAL, new Runnable() {
        @Override
        public void run() {
            if (canvas.isDisposed())
                return;
            // canvas.redraw (); // <-- bad, damages more than is needed
            GC gc = new GC(canvas);
            Point extent = gc.stringExtent(value + '0');
            gc.dispose();
            canvas.redraw(10, 10, extent.x, extent.y, false);
            display.timerExec(INTERVAL, this);
        }
    });
    shell.open();
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch())
            display.sleep();
    }
    image.dispose();
    display.dispose();
}

From source file:TimeStopWhenButtonPressing.java

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);
    label.setBackground(red);/*from  w  w w  .j a v  a 2s  . co m*/
    final int time = 500;
    final Runnable timer = new Runnable() {
        public void run() {
            if (label.isDisposed())
                return;
            Color color = label.getBackground().equals(red) ? blue : red;
            label.setBackground(color);
            display.timerExec(time, this);
        }
    };
    display.timerExec(time, timer);
    button.addListener(SWT.Selection, new Listener() {
        public void handleEvent(Event event) {
            display.timerExec(-1, timer);
        }
    });
    button.pack();
    label.setLayoutData(new RowData(button.getSize()));
    shell.pack();
    shell.open();
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch())
            display.sleep();
    }
    display.dispose();
}

From source file:org.eclipse.swt.snippets.Snippet68.java

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.setText("Snippet 68");
    shell.setLayout(new RowLayout());
    Button button = new Button(shell, SWT.PUSH);
    button.setText("Stop Timer");
    final Label label = new Label(shell, SWT.BORDER);
    label.setBackground(red);/*from  www  .j  a  va  2s.  com*/
    final int time = 500;
    final Runnable timer = new Runnable() {
        @Override
        public void run() {
            if (label.isDisposed())
                return;
            Color color = label.getBackground().equals(red) ? blue : red;
            label.setBackground(color);
            display.timerExec(time, this);
        }
    };
    display.timerExec(time, timer);
    button.addListener(SWT.Selection, event -> display.timerExec(-1, timer));
    button.pack();
    label.setLayoutData(new RowData(button.getSize()));
    shell.pack();
    shell.open();
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch())
            display.sleep();
    }
    display.dispose();
}

From source file:AnimationDoubleBuffering.java

public static void main(String[] args) {
    final Display display = new Display();
    final Shell shell = new Shell(display);
    shell.setText("Animator");

    shell.setLayout(new FillLayout());
    canvas = new Canvas(shell, SWT.NO_BACKGROUND);
    canvas.addPaintListener(new PaintListener() {
        public void paintControl(PaintEvent event) {
            // Create the image to fill the canvas
            Image image = new Image(shell.getDisplay(), canvas.getBounds());
            // Set up the offscreen gc
            GC gcImage = new GC(image);

            gcImage.setBackground(event.gc.getBackground());
            gcImage.fillRectangle(image.getBounds());
            gcImage.setBackground(shell.getDisplay().getSystemColor(SWT.COLOR_RED));
            gcImage.fillOval(x, y, IMAGE_WIDTH, IMAGE_WIDTH);

            // Draw the offscreen buffer to the screen
            event.gc.drawImage(image, 0, 0);

            image.dispose();//from   www.  j av  a  2 s.  c o m
            gcImage.dispose();
        }
    });

    shell.open();
    Runnable runnable = new Runnable() {
        public void run() {
            animate();
            display.timerExec(TIMER_INTERVAL, this);
        }
    };
    display.timerExec(TIMER_INTERVAL, runnable);

    while (!shell.isDisposed()) {
        if (!display.readAndDispatch()) {
            display.sleep();
        }
    }
    // Kill the timer
    display.timerExec(-1, runnable);
    display.dispose();

}

From source file:org.eclipse.swt.snippets.Snippet221.java

public static void main(String[] args) {
    final Display display = new Display();
    Shell shell = new Shell(display);
    shell.setText("Snippet 221");
    shell.setLayout(new FillLayout());
    final Tree tree = new Tree(shell, SWT.FULL_SELECTION | SWT.BORDER);
    tree.setHeaderVisible(true);/*from   w  w  w .ja  va  2s  . c  om*/
    TreeColumn column0 = new TreeColumn(tree, SWT.LEFT);
    column0.setText("Column 0");
    TreeColumn column1 = new TreeColumn(tree, SWT.LEFT);
    column1.setText("Column 1");
    TreeColumn column2 = new TreeColumn(tree, SWT.LEFT);
    column2.setText("Column 2");
    for (int i = 0; i < 9; i++) {
        TreeItem item = new TreeItem(tree, SWT.NONE);
        item.setText("item " + i);
        item.setText(1, "column 1 - " + i);
        item.setText(2, "column 2 - " + i);
        for (int j = 0; j < 9; j++) {
            TreeItem subItem = new TreeItem(item, SWT.NONE);
            subItem.setText("item " + i + " " + j);
            subItem.setText(1, "column 1 - " + i + " " + j);
            subItem.setText(2, "column 2 - " + i + " " + j);
            for (int k = 0; k < 9; k++) {
                TreeItem subsubItem = new TreeItem(subItem, SWT.NONE);
                subsubItem.setText("item " + i + " " + j + " " + k);
                subsubItem.setText(1, "column 1 - " + i + " " + j + " " + k);
                subsubItem.setText(2, "column 2 - " + i + " " + j + " " + k);
            }
        }
    }
    column0.pack();
    column1.pack();
    column2.pack();

    Heartbeat = () -> {
        if (!Tracking || tree.isDisposed())
            return;
        Point cursor = display.getCursorLocation();
        cursor = display.map(null, tree, cursor);
        Scroll(tree, cursor.x, cursor.y);
        display.timerExec(ScrollSpeed, Heartbeat);
    };
    Listener listener = event -> {
        switch (event.type) {
        case SWT.MouseEnter:
            Tracking = true;
            display.timerExec(0, Heartbeat);
            break;
        case SWT.MouseExit:
            Tracking = false;
            break;
        }
    };
    tree.addListener(SWT.MouseEnter, listener);
    tree.addListener(SWT.MouseExit, listener);
    shell.open();
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch())
            display.sleep();
    }
    display.dispose();
}