I am trying to use create a scala program that uses the FXML file generated from JavaFX SceneBuilder for the GUI. Although https://docs.oracle.com/javafx/2/get_started/fxml_tutorial.htm is java, I am using it as my example starting point. (Couldn't find anything better.)
I am able to get this example to run if I use the java version of the controller class, as shown in the example. (Works with either the java version or a scala version of the class that extends Application and has the overridden start method in it.) But if I replace this java version controller class with a scala version, I get the error below:
Exception in thread "JavaFX Application Thread" java.lang.RuntimeException: java.lang.reflect.InvocationTargetException at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1774)
Here is what Intellij converted the java version controller (which I call jController) to a scala version (which I call sController) when I used the menu item Code/Convert Java to Scala:
import javafx.event.ActionEvent
import javafx.fxml.FXML
import javafx.scene.text._
class sController {
@FXML private val actiontarget: Text = null
@FXML protected def handleSubmitButtonAction(event: ActionEvent): Unit = {
actiontarget.setText("Sign in button pressed")
}
}
Of course I also changed <GridPane fx:controller="jController" to <GridPane fx:controller="sController" in the generated FXML file.
I've also tried modifications to the scala version of the controller, starting from the code above that Intellij generated, but nothing worked.
If I replace
@FXML private val actiontarget: Text = null
with
@FXML private val actiontarget: Text = new Text()
then it runs without the exception but does not respond to the button being pressed.
I also tried the javascript version, shown in the example, in place of the controller version, but I don't know anything about javascript and Intellij does not seem to have the "JavaScript and TypeScript bundled plugin" available anymore as referred to here: https://www.jetbrains.com/help/idea/2020.3/javascript-specific-guidelines.html#ws_JavaScript_before_you_start I tried different javascript and typescript plugins, but didn't help.
Any ideas, or is this article correct? https://bugs.openjdk.java.net/browse/JDK-8116127
I managed to get this running as follows:
import javafx.event.ActionEvent
import javafx.fxml.FXML
import javafx.scene.text._
class sController {
@FXML private var actiontarget: Text = _
@FXML protected def handleSubmitButtonAction(event: ActionEvent): Unit = {
actiontarget.setText("Sign in button pressed")
}
}
i.e. use a var field.
I left the field private and still initialized it to _ or null. This is basically as described in programmersdigest.de. But, note that I am not very far with my own application, yet. It could be that the field additionally has to be made public because otherwise the compiler is forced to do name mangling and messes things up. See here for more details.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With