1

I've been trying to get down the basics of using a timer so that I can create a bouncing ball program, but I can't seem to implement a timer correctly. This program should in theory just continuously print the display, but instead the program simply terminates. What can I do to remedy this issue and fix the timer?

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.Timer;
import javax.swing.JFrame;


public class DisplayStuff {

public static void main(String[] args) {


    class TimerListener implements ActionListener
    {
        public void actionPerformed(ActionEvent event)
        {
            System.out.println("Display me.");
        }
    }

    ActionListener listener = new TimerListener();
    Timer t= new Timer(1000, listener);
    t.start();

}
}
  • There are thousands of examples on the internet. bouncing ball is practically java 101 – user1231232141214124 Aug 13 '14 at 23:20
  • 2
    @redFIVE: the original poster's question is a valid question, posted with well formatted, compilable runnable code that demonstrates the issue, something that is rare in a first question post on this site. His problem is more subtle than you make it out to be -- that being why the Timer doesn't run without a Swing GUI being displayed. I think that the question deserves a 1+ up vote and not a down-vote and certainly not criticism. Just my 2 cents. – Hovercraft Full Of Eels Aug 13 '14 at 23:26
  • The differences between `java.util.Timer` and `javax.swing.Timer`... – Kayaman Aug 13 '14 at 23:26
  • @Kayaman: I don't understand the point of your comment. He is using a javax.swing.Timer, and that's what he should be using. – Hovercraft Full Of Eels Aug 13 '14 at 23:27
  • @HovercraftFullOfEels In a full Swing GUI application yes. But in this case the example would work with `java.util.Timer` (as it would be a non-daemon thread preventing the program from ending). – Kayaman Aug 13 '14 at 23:29
  • @Kayaman: but his ultimate goal is to use this timer for a **Swing GUI program**, and thus I stand by my previous comment. If he used a java.util.Timer now, and then used it later in his Swing GUI, he'd run afoul of Swing threading rules. – Hovercraft Full Of Eels Aug 13 '14 at 23:31
  • Sure, and I agree with your comment. I was just commenting on the differences, and the confusing identical name of the timers. – Kayaman Aug 13 '14 at 23:33
  • 1
    It is worth noting the the `Thread` backing the `Timer` is a daemon `Thread`, meaning that when the `main` method exists, the JVM will terminate, so long as there are no more non-daemon threads running, which in your case, there aren't – MadProgrammer Aug 14 '14 at 00:23
  • Thanks to everyone who spent the time to help me. The discussion thread here provided me with more than enough information to understand 'timer'. – Angels and Daemons Aug 14 '14 at 02:14

1 Answers1

3

Your program has no Swing event thread with which to continue the Timer. You need to put it into a visualized Swing GUI to start the Swing event dispatch thread, and then start the timer. This could be achieved by something as simple as displaying a JOptionPane:

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

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

public class DisplayStuff {

   public static void main(String[] args) {

      class TimerListener implements ActionListener {
         public void actionPerformed(ActionEvent event) {
            System.out.println("Display me.");
         }
      }

      ActionListener listener = new TimerListener();
      Timer t = new Timer(1000, listener);
      t.start();

      // ***** add just this *****
      JOptionPane.showMessageDialog(null, "foo");

   }
}
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373