2

Considering this example how do I get or set anything on the form from the Main code?

All we have is a simple Main object like

object Main extends JFXApp {

  val resource = getClass.getResource("/AdoptionForm.fxml")

  val root: jfxs.Parent = jfxf.FXMLLoader.load(resource)

  stage = new PrimaryStage() {
    title = "FXML GridPane Demo"
    scene = new Scene(root)
  }

}

A form defined in FXML.

And a controller class which is never instantiated explicitly.

As far as the controller is a class (not an object) and I don't have a link to its instance in the Main code, how do I access anything on the form?

The final goal I seek to reach is to have an actor that would listen to incoming messages and update the form with the data gotten.

Ivan
  • 63,011
  • 101
  • 250
  • 382

1 Answers1

0

You can get the Controller from the FXML loader.

val loader = new jfxf.FXMLLoader(resource)
val root = loader.load[jfxs.Parent]
val controller = loader.getController[Controller]
ggovan
  • 1,907
  • 18
  • 21
  • `java.lang.ClassCastException: javafx.scene.layout.AnchorPane cannot be cast to scala.runtime.Nothing$` – Ivan Apr 29 '14 at 20:34
  • Which line gives this error? Does inserting the types of the `val`s help? – ggovan Apr 29 '14 at 20:35
  • 1
    The problem is that the `root` type is `Nothing` in this case. It must be `jfxs.Parent` and it needs to be specified explicitly, as I have done in the code I have provided in the question. But if I add `: jfxs.Parent` to `val root` in your example it fails to compile (says `type mismatch; found : T required: javafx.scene.Parent` and `polymorphic expression cannot be instantiated to expected type; found : [T]()T required: javafx.scene.Parent`). – Ivan Apr 29 '14 at 20:41
  • The `load` method has changed since 2.2 (I assume you are using FX8). The method now takes a type signature. I've edited my answer to reflect this. I've not tested it, but it should work. – ggovan Apr 29 '14 at 20:46