0

a couple of weeks ago I started programming in Scala and using Akka Actors. I'm trying to implement a scala programm that uses an Akka ActorSystem to serve multiple UI's. I am currently trying to setup an ScalaFx app and connect it through an internal actor to my actor system. Therefore I coded the following code to setup my actor system and my controller:

object Game {

  // Main class of the game in which the actor system gets started

  def main(args: Array[String]): Unit = {
    val actorSystem = ActorSystem.create("gameActorSystem")
    val controller = actorSystem.actorOf(Props[Controller], "controller")

    var gui = actorSystem.actorOf(Props(new GuiApp.GuiActor(controller)), "guiActor")

    println("Start Game")
    gui ! StartGame

    gui ! "Hello ScalaFx App."
  }
}

class Controller extends Actor {

  private val observers = scala.collection.mutable.SortedSet.empty[ActorRef]

  override def receive: Receive = {
    case StartGame => startGame()
    case RegisterObserver => observers += sender(); sender ! PrintMessage("Subscription from: [" + sender().toString() + "]")
  }

... controller code

}

Then I implemented a simple ScalaFx App:

object GuiApp extends JFXApp {

  class GuiActor(controller: ActorRef) extends Actor {

    controller ! RegisterObserver

    override def receive: Receive = {
      case StartGame => start()
      case s:String => Platform.runLater{
        label.text = "Message: " + s
      }
    }
  }

  def start(): Unit = {
    val args = Array.empty[String]
    main(args)
  }

  val label = new Label("Loading ...")

  stage = new PrimaryStage {
    title = "ScalaFX Game"
    scene = new Scene {
      root = new BorderPane {
        padding = Insets(25)
        center = label
      }
    }
  }
}

So what I'm basically trying to do is to start my scala program, then register the akka system and register the controller and the gui actor. After registering the gui actor I want to start the GUI and then tell the GUI to set the "Hello ScalaFx App." String onto the GUI.

It's working so far, that the ScalaFx GUI starts up, but the initial content of the GUI with "Loading ..." is not being replaced by the "Hello ScalaFx App." String. Can somebody tell me if my approach is correct in any way or am I doing something entirely wrong?

JaySon
  • 367
  • 5
  • 14
  • I suspect a problem with the way ScalaFX initializes app and `Platform.runLater` being called. Do you know what and when does the `main` method that you call in `start` return? Does it return when scalafx has successfully initialized application? After reading API docs of `Platform.runLater` it might be the case that the FX application is not yet fully initialized when you call `Platform.runLater`. You can check that by adding `Thread.sleep(2000)` after `gui ! StartGame`. If that helps, you need to, of course, remove the `sleep` and refactor to ensure proper initialization concurrently. – dvim Jan 15 '18 at 11:59
  • Hey @dvim, thank you for your help back then, I appreciate it. I just changed form ScalaFx to Swing after a day of no responses, which worked fine for me. – JaySon Feb 26 '18 at 02:20

0 Answers0