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 Main() {
    JPanel panel = new JPanel();
    BoxLayout boxLayout = new BoxLayout(panel, BoxLayout.PAGE_AXIS);
    panel.setLayout(boxLayout);// w w  w. j  a  va2 s.c  o m
    for (int i = 0; i < 40; i++) {
        panel.add(new JButton("Button " + i));
    }
    buttons = panel.getComponents();
    activeComponent = buttons[index];
    final JScrollPane scroll = new JScrollPane(panel);

    Timer timer = new Timer(500, new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            ((JButton) activeComponent).setForeground(Color.BLACK);
            if (index >= buttons.length - 1) {
                index = 0;
            } else {
                index++;
            }
            activeComponent = buttons[index];
            ((JButton) activeComponent).setForeground(Color.red);
            setView(scroll, activeComponent);
            System.out.println(((JButton) activeComponent).getActionCommand());
        }
    });
    timer.start();

    scroll.setPreferredSize(new Dimension(200, 300));
    JFrame frame = new JFrame();
    frame.add(scroll);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.pack();
    frame.setVisible(true);
}

From source file:Main.java

/**
 * Creates an animation to fade the dialog opacity from 1 to 0.
 *//*from 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

public void refreshScreen() {
    timer = new Timer(0, new ActionListener() {
        @Override//w w w. ja  v a2s .  co  m
        public void actionPerformed(ActionEvent e) {
            repaint();
        }
    });
    timer.setRepeats(true);
    // Aprox. 60 FPS
    timer.setDelay(17);
    timer.start();
}

From source file:Main.java

public Main() {
    setPreferredSize(new Dimension(320, 240));
    add(odometer);//from ww w  .j a v  a2  s  .c  om
    JComboBox colorBox = new JComboBox(new String[] { "a", "b" });
    colorBox.addActionListener(e -> {
        tabbedPane.setTitleAt(tabbedPane.getSelectedIndex(), "my full new title");
    });
    this.add(colorBox);
    timer = new Timer(250, new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            km += random.nextInt(100);
            odometer.setText(df.format(km));
        }
    });
    timer.start();
}

From source file:ProgressMonitorExample.java

public ProgressMonitorExample() {
    super("Progress Monitor Demo");
    setSize(250, 100);/* w w  w.  j  a va  2  s .com*/
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    pbar = new ProgressMonitor(null, "Monitoring Progress", "Initializing . . .", 0, 100);

    // Fire a timer every once in a while to update the progress.
    Timer timer = new Timer(500, this);
    timer.start();
    setVisible(true);
}

From source file:Main.java

public StringPanel() {
    for (int i = 1; i <= 100; i++) {
        list.add("Word " + i);
    }//from   w  ww  . j  a va  2 s  .com

    word = list.get(0);

    Timer timer = new Timer(500, e -> {
        int rand = random.nextInt(list.size());
        word = list.get(rand);
        x = random.nextInt(D_W - wordWidth);
        y = random.nextInt(D_H) + wordHeight;

        repaint();
    });
    timer.start();
}

From source file:Main.java

public void startAnimation() {
    if (animationTimer == null) {
        currentImage = 0;// ww  w  .  j  av  a  2 s .c  om
        animationTimer = new Timer(animationDelay, this);
        animationTimer.start();
    } else if (!animationTimer.isRunning())
        animationTimer.restart();
}

From source file:Main.java

public BackgroundPane() {
    try {//from  w w w . j av a 2s.  co m
        bg = ImageIO.read(new URL("http://www.java2s.com/style/download.png"));
    } catch (Exception ex) {
        ex.printStackTrace();
    }

    Timer timer = new Timer(40, new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            yOffset += yDelta;
            if (yOffset > getHeight()) {
                yOffset = 0;
            }
            repaint();
        }
    });
    timer.start();
}

From source file:raspihomeapp.ParamForm.java

/**
 * Creates new form ParamForm/*from   w  ww. j ava  2  s .  c  o  m*/
 */
public ParamForm() {
    initComponents();
    timer = new Timer(60000, taskPerformer);
    timer.setInitialDelay(0);
    timer.start();
    Plotxy();

}

From source file:MultiKeyCombo.java

public MultiKeySelectionManager() {
    resetTimer = new Timer(RESET_DELAY, new ActionListener() {
        public void actionPerformed(ActionEvent actionEvent) {
            currentSearch.setLength(0);/*from   www  .  jav  a2  s .c om*/
        }
    });
}