0

Taking from Mucaho's Scalatrix example I'd like to send messages from the View (ScalaFX) to the controller actor, how can I abstract/expose the actor to be able to do this?

object Ops extends App {

  override def main(args: Array[String]): Unit = {
    new JFXPanel(); // trick: create empty panel to initialize toolkit
    new Thread(new Runnable() {
      override def run(): Unit = {
        View.main(Array[String]())
      }
    }).start()

    val system = ActorSystem("Ops")
    val controller = system.actorOf(Props[Controller], "controller")
  }
}
pagoda_5b
  • 7,333
  • 1
  • 27
  • 40
  • I'm not clear on what you're asking here. From the `ActorSystem`, you can always obtain a reference to any actor therein, using the method [`actorSelection(path: String)`](http://doc.akka.io/api/akka/2.3.9/?_ga=1.251897226.2043915.1425983990#akka.actor.ActorSystem) and passing the name path you gave to the actor (i.e. `"controller"`) – pagoda_5b Mar 10 '15 at 10:42
  • For the record, which version of akka/scala are you using? – pagoda_5b Mar 10 '15 at 10:44
  • @pagoda_5b I could instantiate the ActorSystem from within the View as follows: `object View extends JFXApp { val system = ActorSystem("Ops") val controller = system.actorOf(Props[Controller], "controller") stage = new PrimaryStage { title = "Ops" width = 800 height = 600 minWidth = 800 minHeight = 600 scene = {...} } }` but not sure how I'd be able to access it from the View if the ActorSystem was defined as in my inital post? – Daniel de la Hoya Mar 12 '15 at 09:15

1 Answers1

0

The only thing that comes to my mind at the moment is to wrap the system and actors as publicly accessible members of another object

object Ops extends App {

  override def main(args: Array[String]): Unit = {
    new JFXPanel(); // trick: create empty panel to initialize toolkit
    new Thread(new Runnable() {
      override def run(): Unit = {
        View.main(Array[String]())
      }
    }).start()

    object Actors {
      val system = ActorSystem("Ops")
      val controller = system.actorOf(Props[Controller], "controller")
    }
  }
}

now you can send messages in any part of your code, e.g.

object View extends JFXApp {
  stage = ...

  Ops.Actors.controller ! Tick
}
pagoda_5b
  • 7,333
  • 1
  • 27
  • 40