16

Java code:

import javax.swing.Timer;
class Main { 
    public static void main(String args[]) {
    MyListener myListener = new MyListener();
        Timer timer = new Timer(1000, myListener);
    timer.start();
        while(timer.isRunning()) {
            System.out.print(".");
        }
    }
}

Scala code:

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
class MyListener extends ActionListener {
    override def actionPerformed(arg0: ActionEvent) {
        println("Do something");  
    }
}

Command line:

scalac MyListener.scala
javac Main.java
java -cp /usr/share/java/scala-library.jar:. Main
givanse
  • 14,503
  • 8
  • 51
  • 75
  • 2
    It should work fine. What happens when you try? – skaffman Jul 24 '09 at 18:54
  • I didn't know where to start and from what I read through google-ing it seemed a bit more complex. Damn OLD posts(2008), I'm glad Scala has been under very active development. I updated my question with what I did. – givanse Jul 24 '09 at 19:33

1 Answers1

14

I'd start by using java.util.Timer - not javax.swing.Timer. The swing timer won't work unless you are running your app with a GUI (ie. it won't work if you run it on Linux through a console without a special command line parameter - best avoided).

Setting that aside:

  1. Be sure, that when you try to run the code, you include scala-library.jar on your classpath.

  2. Don't forget to start the timer - timer.start()

This code worked fine for me (the Scala code required no modification):

MyListener myListener = new MyListener();
Timer timer = new Timer(1000, myListener);
timer.start();
Thread.sleep(10000);
sanity
  • 35,347
  • 40
  • 135
  • 226
  • According to the jdk-6-doc only javax.swing.Timer receives an ActionListener in its constructor and I need it to fire some other actions. – givanse Jul 24 '09 at 19:30