Example usage for javax.swing Timer setDelay

List of usage examples for javax.swing Timer setDelay

Introduction

In this page you can find the example usage for javax.swing Timer setDelay.

Prototype

public void setDelay(int delay) 

Source Link

Document

Sets the Timer's between-event delay, the number of milliseconds between successive action events.

Usage

From source file:Main.java

/**
 * Creates an animation to fade the dialog opacity from 0 to 1, wait at 1
 * and then fade to 0 and dispose.//w  w  w .  ja  v a 2s .  c  o m
 *
 * @param dialog the dialog to display
 * @param delay the delay in ms before starting and between each change
 * @param incrementSize the increment size
 * @param displayTime the time in ms the dialog is fully visible
 */
public static void fadeInAndOut(final JDialog dialog, final int delay, final float incrementSize,
        final int displayTime) {
    final Timer timer = new Timer(delay, null);
    timer.setRepeats(true);
    timer.addActionListener(new ActionListener() {
        private float opacity = 0;
        private boolean displayed = false;

        @Override
        public void actionPerformed(ActionEvent e) {

            if (!displayed) {
                opacity += incrementSize;
                dialog.setOpacity(Math.min(opacity, 1)); // requires java 1.7
                if (opacity >= 1) {
                    timer.setDelay(displayTime);
                    displayed = true;
                }
            } else {
                timer.setDelay(delay);
                opacity -= incrementSize;
                dialog.setOpacity(Math.max(opacity, 0)); // requires java 1.7
                if (opacity < 0) {
                    timer.stop();
                    dialog.dispose();
                }
            }
        }
    });

    dialog.setOpacity(0); // requires java 1.7
    timer.start();
    dialog.setVisible(true);
}

From source file:fr.duminy.jbackup.core.JBackupImpl.java

@Override
public Timer shutdown(final TerminationListener listener) throws InterruptedException {
    executor.shutdown();//w  w  w  . j  a v a2  s .  c o m

    Timer timer = null;
    if (listener != null) {
        timer = new Timer(0, null);
        timer.setDelay((int) TimeUnit.SECONDS.toMillis(1));
        final Timer finalTimer = timer;
        timer.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if (executor.isTerminated()) {
                    listener.terminated();
                    finalTimer.stop();
                }
            }
        });
        timer.setRepeats(true);
        timer.start();
    }

    return timer;
}