0

I'm still new to java and followed tutorials online from multiple sites for a timer I want to make for my GUI program but I wanted to test out the timer on a standard loop first.

import javax.swing.*;
import java.awt.*;

class main {
    public static void main(String Args[]) {
        ActionListener changelight = new ActionListener();
        int delay = 1000;
        Timer timer = new Timer(delay, changelight);
        timer.start();
    }

    public void actionPerformed(ActionEvent e) {
            for (int a=0; a<=1000; a++) {
            System.out.printf("%d \n", a);
            }
        return;
    }
}

I can't seem to get it to run properly and hoping one of use can see why?

Thanks

Mike Clark
  • 10,027
  • 3
  • 40
  • 54
lecardo
  • 1,208
  • 4
  • 15
  • 37

2 Answers2

2

ActionListener is an interface, not a class which means that you must implement the method(s) as shown below:

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

public class Main {

    public static void main(String args[]){
        final ActionListener action = new ActionListener(){
            public void actionPerformed(final ActionEvent e){
                for(int i = 0; i <= 1000; i++)
                    System.out.printf("%d\n", i);
            }
        };
        final Timer timer = new Timer(1000, action);
        timer.start();
    }
}

Alternatively, if you are using Java 8, perhaps you could try something like:

    public static void main(String args[]){
        final ActionListener listener = e -> {
            for(int i = 0; i <= 1000; i++)
                System.out.printf("%d\n", i);
        };
        final Timer timer = new Timer(1000, listener);
        timer.start();
    }
Josh M
  • 11,611
  • 7
  • 39
  • 49
  • Since the OP wanted to only test the timer you should place a `Thread.sleep(5000);` after the `timer.start();` so that the program won't exit which kills the timer of course. – vallentin Oct 23 '13 at 20:21
  • Why do you use "final" before actionlistener. What benefit does that imply ? – lecardo Oct 23 '13 at 20:21
  • @Bobski Read the answers [here](http://programmers.stackexchange.com/questions/98691/excessive-use-final-keyword-in-java). – Josh M Oct 23 '13 at 20:24
  • +1 for Java 8 Lambda Expression, did not know about them before now! – vallentin Oct 23 '13 at 20:31
  • right the program executes fine but doesn't run anything from the loop – lecardo Oct 23 '13 at 20:36
  • @Bobski Wrap the whole body of main in a call to `SwingUtilities.invokeAndWait()`. That will block `main()` so the `Timer` thread can run. Take a look at [invokeAndWait method in SwingUtilities](http://stackoverflow.com/questions/5499921/invokeandwait-method-in-swingutilities) for a detailed example of how to do this – Jason Braucht Oct 23 '13 at 20:51
1

Your example does not work because ActionListener is an interface and you can't instantiate it. Instead you need to create your class which will implement it ( or add implements ActionListener to your class main and then implement actionPerformed )

And even if you have that right your program will finish earlier than first event will come so the actionPerformed will not be called. To observe something you need piece of code that will 'prevent your program from reaching the end too soon' and for that you can use simple JFrame object.

Here is working example:

import javax.swing.*;

import java.awt.*;
import java.awt.event.*;

class TimerAction implements ActionListener{

    boolean isItTimeForTic = true;

   @Override public void actionPerformed(ActionEvent e) {
      if( isItTimeForTic ) {
         isItTimeForTic = false;
            System.out.println("tic ");
      }
      else {
         isItTimeForTic = true;
         System.out.println("tac ");
      }
   }
}

public class TimerTest {

   public static void main(String Args[] ) {
      int delay = 1000;
      Timer timer = new Timer(delay, new TimerAction());

      timer.start();
      JFrame frame = new JFrame();
      frame.setVisible(true);
   }
}
Aubin
  • 14,617
  • 9
  • 61
  • 84
Luke
  • 1,236
  • 2
  • 11
  • 16