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:TimerChangeButtonBackground.java

public TimerChangeButtonBackground() {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    getContentPane().add(button, "Center");
    button.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            timer.stop();//ww w . j  ava 2 s. c  o m
            button.setBackground(Color.red);
        }
    });
    timer = new Timer(1000, new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            button.setBackground(flag ? Color.green : Color.yellow);
            flag = !flag;
            repaint();
        }
    });
    timer.start();
    pack();
    setVisible(true);
}

From source file:Main.java

public ClockPane() {
    setLayout(new BorderLayout());
    tickTock();/*from   www  . j  a  v a2 s. c o m*/
    add(clock);
    Timer timer = new Timer(500, new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            tickTock();
        }
    });
    timer.setRepeats(true);
    timer.setCoalesce(true);
    timer.setInitialDelay(0);
    timer.start();
}

From source file:TimerBasedAnimation.java

public TimerBasedAnimation() {
    setXY(20 * Math.random(), 200, 200);

    timer = new Timer(20, this);
    timer.setInitialDelay(190);
    timer.start();
}

From source file:Main.java

public Main() {
    this.setLayout(new GridLayout(0, 1));
    this.add(label);
    this.add(new JButton(new AbstractAction("OK") {

        @Override//from w  w  w  .  j av  a2s  . c  o m
        public void actionPerformed(ActionEvent e) {
            JButton b = (JButton) e.getSource();
            b.setText("123");
        }
    }));
    new Timer(100, new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            label.setText(String.valueOf(System.nanoTime()));
        }
    }).start();
}

From source file:Main.java

public MyCanvas() {
    Timer timer = new Timer(30, new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            x1 += 2;//from  w ww . j a  va  2s  .com
            x2 += 2;
            repaint();
        }
    });
    timer.start();
}

From source file:Main.java

public TestPane() {
    setLayout(new GridBagLayout());
    label = new JLabel();
    add(label);//  w  w w  . j a v a2  s. c  o  m
    Timer timer = new Timer(1000, new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            String labelText = label.getText();
            labelText += text.charAt(charIndex);
            label.setText(labelText);
            charIndex++;
            if (charIndex >= text.length()) {
                ((Timer) e.getSource()).stop();
            }
        }
    });
    timer.start();
}

From source file:Main.java

public Main() {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setSize(300, 200);//from  w w  w.  j  av a2s .com
    blocker = new JDialog(this, true);
    blocker.setLayout(new FlowLayout());
    blocker.add(new JLabel("I'm blocking EDT!"));
    JProgressBar progress = new JProgressBar();
    progress.setIndeterminate(true);
    blocker.add(progress);
    blocker.pack();

    timer = new Timer(3000, new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            doSomeWork();
        }
    });
    timer.setRepeats(false);
    timer.start();
}

From source file:Main.java

public BlinkPane() {
    label = new JLabel("Hello");
    setLayout(new GridBagLayout());

    add(label);//from w w  w .  j  a  v  a2s.  c  o m
    Timer timer = new Timer(1000, new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent ae) {
            on = !on;
            repaint();
        }
    });
    timer.setRepeats(true);
    timer.setCoalesce(true);
    timer.start();
}

From source file:Main.java

public TestPane() {
    label = new JLabel("...");
    setLayout(new GridBagLayout());
    add(label);/*from  w ww.j a  v a2  s . c  o m*/
    timer = new Timer(500, new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            count++;
            if (count < 100000) {
                label.setText(Integer.toString(count));
            } else {
                ((Timer) (e.getSource())).stop();
            }
        }
    });
    timer.setInitialDelay(0);
    timer.start();
}

From source file:Main.java

public Main() {
    for (int i = 0; i < panels.length; i++) {
        final String[] labels = new String[] { "0", "1" };
        final Random rand = new Random();
        int index = rand.nextInt(labels.length);
        String randomTitle = labels[index];
        final JLabel label = new JLabel(randomTitle, JLabel.CENTER);
        Timer lblt = new Timer(00, new ActionListener() {
            @Override//  w  ww.  j  ava2 s.  c o  m
            public void actionPerformed(ActionEvent ae) {
                label.setText(labels[rand.nextInt(labels.length)]);
            }
        });
        lblt.setRepeats(true);
        lblt.start();
        label.setForeground(Color.green);
        label.setVerticalAlignment(JLabel.CENTER);
        panels[i] = new JPanel();
        panels[i].setBackground(Color.BLACK);
        panels[i].add(label);
        frame.getContentPane().add(panels[i]);
    }
    frame.setLayout(new GridLayout(grid, grid));
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setExtendedState(java.awt.Frame.MAXIMIZED_BOTH);
    frame.setVisible(true);

    ActionListener action = new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            for (int i = 0; i < panels.length; i++) {
                Color mix = new Color(255, 255, 255);
                Random random = new Random();
                int r = random.nextInt(255);
                int g = random.nextInt(255);
                int b = random.nextInt(255);

                if (mix != null) {
                    r = (r + mix.getRed()) / 2;
                    g = (g + mix.getGreen()) / 2;
                    b = (b + mix.getBlue()) / 2;
                }
                Color color = new Color(r, g, b);
                panels[i].setBackground(color);
            }
        }
    };

    t = new Timer(00, action);
    t.setRepeats(true);
    t.start();
}