Valerin
Valerin

Reputation: 475

scala - timer sample

I have this piece of code, trying to build a timer that will decrease a given value in a TextField (which is supposed to contain minutes needed to finish a job, but those minutes will be given manually and then be decreased by this clock):

import scala.swing._

class ScalaTimer(val delay: Int) {
  val tmr: javax.swing.Timer = new javax.swing.Timer(delay, null)
  def start() = tmr.start()
  def stop() = tmr.stop()
}

object Test33 { //extends SimpleSwingApplication {
  val timer = new ScalaTimer(50)
  timer.tmr.start
  //def top = new MainFrame {
 def main(args: Array[String]) {
    timer.tmr.addActionListener(Swing.ActionListener(e => {
    println(timer.delay - 1)
    }))
 }
 //}
}

I don't get why it doesn't print anything when I use a main() method, but it prints the current given delay when I use a Frame :|

Upvotes: 3

Views: 4042

Answers (1)

chiastic-security
chiastic-security

Reputation: 20520

It won't print anything with your code as it stands because your application exits as soon as it's added the ActionListener, and before anything has had a chance to fire it!

Try adding

Thread.sleep(10000);

just before the end of your main method, and you'll find it'll print 49 repeatedly.

It works as it stands with a Frame because that prevents the application from terminating until the Frame is closed.

Upvotes: 3

Related Questions