tags:

views:

998

answers:

1

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
+4  A: 

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
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.
Gastoni