Example usage for javax.swing Timer Timer

List of usage examples for javax.swing Timer Timer

Introduction

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

Prototype

public Timer(int delay, ActionListener listener) 

Source Link

Document

Creates a Timer and initializes both the initial delay and between-event delay to delay milliseconds.

Usage

From source file:Main.java

public static void fadeIn(final Dialog win) {
    if (!win.isUndecorated()) {
        return;//www .  jav a  2  s .  c  o  m
    }
    final Timer timer = new Timer(30, null);
    timer.setRepeats(true);
    timer.addActionListener(new ActionListener() {
        private float opacity = 0;

        @Override
        public void actionPerformed(ActionEvent e) {
            opacity += 0.05f;
            win.setOpacity(Math.min(opacity, 1f));
            if (opacity >= 1) {
                timer.stop();
            }
        }
    });
    win.setOpacity(0);
    timer.start();
    win.setVisible(true);
}

From source file:Main.java

/**
 * Creates an animation to fade the dialog opacity from 0 to 1.
 *///  w w w . j a va  2  s  .c o m
public static void fadeIn(final JDialog dialog) {
    final Timer timer = new Timer(10, null);
    timer.setRepeats(true);
    timer.addActionListener(new ActionListener() {

        private float opacity = 0;

        @Override
        public void actionPerformed(ActionEvent e) {
            opacity += 0.15f;
            dialog.setOpacity(Math.min(opacity, 1));
            if (opacity >= 1) {
                timer.stop();
            }
        }
    });

    dialog.setOpacity(0);
    timer.start();
    dialog.setVisible(true);
}

From source file:Main.java

private static void triggerBringToFront(final JFrame f, final int delayMS) {
    Timer timer = new Timer(delayMS, new ActionListener() {
        @Override//from  w  ww .j a  v a  2s.c om
        public void actionPerformed(ActionEvent e) {
            // This will only cause the task bar entry
            // for this frame to blink
            // f.toFront();

            // This will bring the window to the front,
            // but not make it the "active" one
            // f.setAlwaysOnTop(true);
            // f.setAlwaysOnTop(false);

            if (!f.isActive()) {
                f.setState(JFrame.ICONIFIED);
                f.setState(JFrame.NORMAL);
            }
        }
    });
    timer.setRepeats(false);
    timer.start();
}

From source file:Main.java

/**
 * Creates an animation to fade the dialog opacity from 0 to 1.
 *
 * @param dialog the dialog to fade in//from w ww . jav a  2 s. c  om
 * @param delay the delay in ms before starting and between each change
 * @param incrementSize the increment size
 */
public static void fadeIn(final JDialog dialog, int delay, final float incrementSize) {
    final Timer timer = new Timer(delay, null);
    timer.setRepeats(true);
    timer.addActionListener(new ActionListener() {
        private float opacity = 0;

        @Override
        public void actionPerformed(ActionEvent e) {
            opacity += incrementSize;
            dialog.setOpacity(Math.min(opacity, 1)); // requires java 1.7
            if (opacity >= 1) {
                timer.stop();
            }
        }
    });

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

From source file:Main.java

/**
 * Creates an animation to fade the dialog opacity from 1 to 0, and then
 * dispose./*ww w  .  j a v  a  2 s. c  o  m*/
 *
 * @param dialog the dialog to fade out
 * @param delay the delay in ms before starting and between each change
 * @param incrementSize the increment size
 */
public static void fadeOut(final JDialog dialog, int delay, final float incrementSize) {
    final Timer timer = new Timer(delay, null);
    timer.setRepeats(true);
    timer.addActionListener(new ActionListener() {
        private float opacity = 1;

        @Override
        public void actionPerformed(ActionEvent e) {
            opacity -= incrementSize;
            dialog.setOpacity(Math.max(opacity, 0)); // requires java 1.7
            if (opacity < 0) {
                timer.stop();
                dialog.dispose();
            }
        }
    });

    dialog.setOpacity(1); // requires java 1.7
    timer.start();
}

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./*from   www  .  j a  v a 2 s .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:Main.java

/** 
 * Runs the supplied class after a certain period of time, the thread
 * will be executed in the EDT. //w ww.  j  a v a 2 s .  c  o  m
 *
 * @param execute The runnable object whose method will be called after the
 * specified delay
 * @param after The delay in ms before the event will be called
 */
public static Timer invokeAfter(final Runnable execute, int after) {
    Timer timer = new Timer(after, new ActionListener() {
        public void actionPerformed(ActionEvent actionEvent) {
            execute.run();
        }
    });
    timer.setRepeats(false);
    timer.start();
    return timer;
}

From source file:TickTockInner.java

private void go() {
    Timer t = new Timer(1000, new Ticker());
    t.start();//from   w  w  w  .java 2s.c om
    JOptionPane.showMessageDialog(null, "Click OK to exit program");
    System.exit(0);
}

From source file:Main.java

private static void createTimer() {
    if (timer != null) {
        return;/*w  ww  .  ja  v a 2  s  . co m*/
    }
    timer = new Timer(20, new ActionListener() {
        public void actionPerformed(ActionEvent actionEvent) {
        }
    });
    timer.setDelay(20);
    timer.setInitialDelay(20);
    timer.setCoalesce(true);
    timer.setRepeats(true);
    timer.start();
}

From source file:Main.java

public Main() {
    frame.setSize(600, 600);// w  w w.  ja  v a2  s.c o  m
    frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
    frame.setVisible(true);
    frame.setExtendedState(JFrame.ICONIFIED);
    Timer t = new Timer(3000, new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent arg0) {
            java.awt.EventQueue.invokeLater(new Runnable() {
                @Override
                public void run() {
                    frame.setExtendedState(JFrame.NORMAL);
                }
            });
        }
    });
    t.setRepeats(false);
    t.start();
}