I have cut my problem down to simple lines of Scala + JavaFX 2’s FXMLLoader:
val path = "/com/myapp/views/main.fxml"
val loader = new FXMLLoader()
loader.setLocation(getClass.getResource(path))
val root = loader.load(getClass.getResourceAsStream(path)).asInstanceOf[Parent]
I am trying to load a main.fxml file using FXMLLoader, but I end up with:
Class javafx.fxml.FXMLLoader$ValueElement can not access a member of
class com.myapp.controllers.MainWindow with modifiers “private”
The FXML code looks like:
<?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.*?>
<?import java.net.*?>
<?import java.util.*?>
<?import javafx.collections.*?>
<?import javafx.geometry.*?>
<?import javafx.scene.Scene?>
<?import javafx.scene.control.*?>
<?import javafx.scene.effect.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.paint.*?>
<?import javafx.scene.text.*?>
<BorderPane fx:controller="com.myapp.controllers.MainWindow" fx:id="mainWindow" prefHeight="703.0" prefWidth="803.0" xmlns:fx="http://javafx.com/fxml">
<stylesheets>
<URL value="@../styles/Styles.css" />
</stylesheets>
<top>
...
According to the error message, JavaFX’s FXMLLoader tries to access some property of my controller which is private. However, I have no private members in the controller:
class MainWindow extends Initializable {
override def initialize(location: URL, resourceBundle: java.util.ResourceBundle) {
print("init")
}
}
What could be the problem?
The error points to a private constructor. Do you need to explicitly make a constructor public in Scala? You can use javap to see what is compiled.