Reputation: 903
I am trying to pass a stored preference value to a text box in a settings window that can be opened from a user login window. I am planning to do this by setting the value in the controller prior to open. As you can see, I am also trying to make the settings window a child of the login window. However, I am getting javafx.scene.layout.AnchorPane cannot be cast to javafx.fxml.FXMLLoader
for reasons that I don't understand and am completely at a loss as to what to do.
My code for opening the settings window on the press of a button is as follows:
@FXML
void OpenSettingsWindow(ActionEvent event) {
try {
FXMLLoader fxmlLoader = new FXMLLoader();
fxmlLoader = FXMLLoader.load(SettingsWindowController.class
.getResource("Settings.fxml"));
AnchorPane page = (AnchorPane) FXMLLoader.load(SettingsWindowController.class
.getResource("Settings.fxml"));
Scene scene = new Scene(page);
root = new Stage();
root.initModality(Modality.WINDOW_MODAL);
root.initOwner(Main.primaryStage);
root.setScene(scene);
SettingsWindowController controller = fxmlLoader.getController();
String databaseAddressValue = "databaseAddressValue";
controller.setDatabaseAddressValue(Preferences
.systemRoot()
.node("preferences.SystemPreferences")
.get(SystemPreferences.databaseAddress, databaseAddressValue));
root.show();
} catch (Exception e) {
e.printStackTrace();
}
Any advice as to how to fix this is much appreciated.
Upvotes: 0
Views: 1422
Reputation: 114
Try this :
FXMLLoader fxmlLoader = FXMLLoader(SettingsWindowController.class.getResource("Settings.fxml"));
AnchorPane page = (AnchorPane) fxmlLoader.load();
Upvotes: 0
Reputation: 2179
You are assigning the return value of FXMLLoader.load()
to a FXMLLoader
reference.
FXMLLoader.load()
returns the highest object in you FXML file and that is for sure not a FXMLLoader
object.
If you want to use a controller class for event handling and proper intialization you have to set it first and load the FXML in an other way (I assume that the SettingsWindowController
is your controller class and has a default constructor):
SettingsWindowController controller = new SettingsWindowController();
FXMLLoader loader = new FXMLLoader(SettingsWindowController.class
.getResource("Settings.fxml"));
loader.setController(controller);
AnchorPane page = (AnchorPane)loader.load();
Upvotes: 1