Use Swing Timer class in Java

Description

The following code shows how to use Swing Timer class.

A Timer object will send an ActionEvent to the registered ActionListener.

Example


/* w ww  .j a va 2  s.c  om*/

   


import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.Timer;

public class Main implements ActionListener {
  Timer t = new Timer(1000, this);

  Main() {
    t.start();
  }

  public void actionPerformed(ActionEvent e) {
    System.out.println("working");
  }
  public static void main(String args[]) {
    Main main = new Main();
  }
}




















Home »
  Java Tutorial »
    Swing »




Action
Border
Color Chooser
Drag and Drop
Event
Font Chooser
JButton
JCheckBox
JComboBox
JDialog
JEditorPane
JFileChooser
JFormattedText
JFrame
JLabel
JList
JOptionPane
JPasswordField
JProgressBar
JRadioButton
JScrollBar
JScrollPane
JSeparator
JSlider
JSpinner
JSplitPane
JTabbedPane
JTable
JTextArea
JTextField
JTextPane
JToggleButton
JToolTip
JTree
Layout
Menu
Timer