Example usage for javax.swing Timer stop

List of usage examples for javax.swing Timer stop

Introduction

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

Prototype

public void stop() 

Source Link

Document

Stops the Timer, causing it to stop sending action events to its listeners.

Usage

From source file:MainClass.java

License:asdf

public static void main(String[] args) {
    Timer timer = new Timer(1000, new MyTimerActionListener());

    timer.start();//  w  ww  .ja  v a2 s . c o  m
    try {
        Thread.sleep(10000);
    } catch (InterruptedException e) {
    }
    timer.stop();
}

From source file:Main.java

public static void main(String[] args) {
    Runnable r = new Runnable() {
        @Override/*from   ww  w.  j a  va 2  s .c o  m*/
        public void run() {
            JPanel gui = new JPanel();

            final AnimatedImage[] tiles = new AnimatedImage[2];
            for (int ii = 0; ii < tiles.length; ii++) {
                tiles[ii] = new AnimatedImage();
                gui.add(new JLabel(new ImageIcon(tiles[ii])));
            }
            ActionListener listener = new ActionListener() {

                @Override
                public void actionPerformed(ActionEvent e) {
                    for (int i = 0; i < tiles.length; i++) {
                        tiles[i].paintImage();
                        gui.repaint();
                    }
                }
            };
            Timer timer = new Timer(50, listener);
            timer.start();

            JOptionPane.showMessageDialog(null, gui);
            timer.stop();
        }
    };
    SwingUtilities.invokeLater(r);
}

From source file:Main.java

public static void main(String[] args) {
    JProgressBar progressBar = new JProgressBar();
    JButton button = new JButton("Start");
    JFrame f = new JFrame();
    f.setLayout(new FlowLayout());
    f.add(progressBar);/*from w  ww . ja  va2 s  . c  o m*/
    f.add(button);

    ActionListener updateProBar = new ActionListener() {
        public void actionPerformed(ActionEvent actionEvent) {
            int val = progressBar.getValue();
            if (val >= 100) {
                //  timer.stop();
                button.setText("End");
                return;
            }
            progressBar.setValue(++val);
        }
    };
    Timer timer = new Timer(50, updateProBar);
    button.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            if (timer.isRunning()) {
                timer.stop();
                button.setText("Start");
            } else if (button.getText() != "End") {
                timer.start();
                button.setText("Stop");
            }
        }
    });
    f.pack();
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.setVisible(true);
}

From source file:Main.java

public static void main(String[] args) {
    JFrame frame = new JFrame("Testing");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setLayout(new BorderLayout());
    frame.add(new JLabel("Auto Hide"));
    frame.pack();//w w  w.  j  av  a2s. com
    frame.setVisible(true);

    Timer autoHideTimer = new Timer(1000, new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            frame.dispose();
        }
    });
    autoHideTimer.setRepeats(false);

    frame.addMouseListener(new MouseAdapter() {
        @Override
        public void mouseExited(MouseEvent e) {
            System.out.println("Restart...");
            autoHideTimer.restart();
        }

        @Override
        public void mouseEntered(MouseEvent e) {
            System.out.println("Stop");
            autoHideTimer.stop();
        }
    });
}

From source file:Main.java

public static void main(String[] args) {
    final Timer timer;
    final JProgressBar progressBar = new JProgressBar();
    final JButton button = new JButton("Start");
    JFrame f = new JFrame();
    f.setLayout(new FlowLayout());
    f.add(progressBar);/*from   ww w  .  j  ava  2  s.co  m*/
    f.add(button);

    ActionListener updateProBar = new ActionListener() {
        public void actionPerformed(ActionEvent actionEvent) {
            int val = progressBar.getValue();
            if (val >= 100) {
                //  timer.stop();
                button.setText("End");
                return;
            }
            progressBar.setValue(++val);
        }
    };
    timer = new Timer(50, updateProBar);
    button.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            if (timer.isRunning()) {
                timer.stop();
                button.setText("Start");
            } else if (button.getText() != "End") {
                timer.start();
                button.setText("Stop");
            }
        }
    });
    f.pack();
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.setResizable(false);
    f.setLocationRelativeTo(null);
    f.setVisible(true);
}

From source file:Main.java

/**
 * Creates an animation to fade the dialog opacity from 0 to 1.
 *//*  w w  w  .j av  a  2s  .c  om*/
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

public static void fadeIn(final Dialog win) {
    if (!win.isUndecorated()) {
        return;/*from   w  w  w .j a va2 s. c om*/
    }
    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 1 to 0.
 *///  w  w w.j a v  a  2 s .c  o m
public static void fadeOut(final JDialog dialog) {
    final Timer timer = new Timer(10, null);
    timer.setRepeats(true);
    timer.addActionListener(new ActionListener() {
        private float opacity = 1;

        @Override
        public void actionPerformed(ActionEvent e) {
            opacity -= 0.15f;
            dialog.setOpacity(Math.max(opacity, 0));
            if (opacity <= 0) {
                timer.stop();
                dialog.dispose();
            }
        }
    });

    dialog.setOpacity(1);
    timer.start();
}

From source file:Main.java

/**
 * Creates an animation to fade the dialog opacity from 0 to 1.
 *//*w w w  .  j a v a 2 s.com*/
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

/**
 * Creates an animation to fade the dialog opacity from 1 to 0, and then
 * dispose.//from w w  w.  j  a  va  2s. c  om
 *
 * @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();
}