Why Timer does not work if we do not generate a window?

Posted by Roman on Stack Overflow See other posts from Stack Overflow or by Roman
Published on 2010-03-19T15:30:35Z Indexed on 2010/03/19 15:31 UTC
Read the original article Hit count: 144

Filed under:
|
|
|

Here is the code:

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

import javax.swing.JFrame;
import javax.swing.Timer;

public class TimerSample {
  public static void main(String args[]) {
    new JFrame().setVisible(true);
    ActionListener actionListener = new ActionListener() {
      public void actionPerformed(ActionEvent actionEvent) {
        System.out.println("Hello World Timer");
      }
    };
    Timer timer = new Timer(500, actionListener);
    timer.start();
  }
}

It generates a window and then periodically prints "Hello World Timer" in the terminal (Command Prompt). If I comment this line new JFrame().setVisible(true); the application do not print anything to the command line. Why?

© Stack Overflow or respective owner

Related posts about java

Related posts about swing