如何在 JavaFX 中将值传递给 Window
How to Pass Values to Window in JavaFX
我正在尝试将存储的首选项值传递到设置 window 中的文本框,该文本框可以通过用户登录 window 打开。我计划通过在打开之前在控制器中设置值来做到这一点。如您所见,我也在尝试使设置 window 成为登录名 window 的子项。但是,由于我不明白的原因,我得到 javafx.scene.layout.AnchorPane cannot be cast to javafx.fxml.FXMLLoader
并且完全不知道该怎么做。
我按下按钮打开设置 window 的代码如下:
@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();
}
非常感谢任何有关如何解决此问题的建议。
您正在将 FXMLLoader.load()
的 return 值分配给 FXMLLoader
引用。
FXMLLoader.load()
return 是 FXML 文件中最高的对象,肯定不是 FXMLLoader
对象。
如果您想使用控制器 class 进行事件处理和适当的初始化,您必须先设置它并以其他方式加载 FXML(我假设 SettingsWindowController
是您的控制器class 并有一个默认构造函数):
SettingsWindowController controller = new SettingsWindowController();
FXMLLoader loader = new FXMLLoader(SettingsWindowController.class
.getResource("Settings.fxml"));
loader.setController(controller);
AnchorPane page = (AnchorPane)loader.load();
试试这个:
FXMLLoader fxmlLoader = FXMLLoader(SettingsWindowController.class.getResource("Settings.fxml"));
AnchorPane page = (AnchorPane) fxmlLoader.load();
我正在尝试将存储的首选项值传递到设置 window 中的文本框,该文本框可以通过用户登录 window 打开。我计划通过在打开之前在控制器中设置值来做到这一点。如您所见,我也在尝试使设置 window 成为登录名 window 的子项。但是,由于我不明白的原因,我得到 javafx.scene.layout.AnchorPane cannot be cast to javafx.fxml.FXMLLoader
并且完全不知道该怎么做。
我按下按钮打开设置 window 的代码如下:
@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();
}
非常感谢任何有关如何解决此问题的建议。
您正在将 FXMLLoader.load()
的 return 值分配给 FXMLLoader
引用。
FXMLLoader.load()
return 是 FXML 文件中最高的对象,肯定不是 FXMLLoader
对象。
如果您想使用控制器 class 进行事件处理和适当的初始化,您必须先设置它并以其他方式加载 FXML(我假设 SettingsWindowController
是您的控制器class 并有一个默认构造函数):
SettingsWindowController controller = new SettingsWindowController();
FXMLLoader loader = new FXMLLoader(SettingsWindowController.class
.getResource("Settings.fxml"));
loader.setController(controller);
AnchorPane page = (AnchorPane)loader.load();
试试这个:
FXMLLoader fxmlLoader = FXMLLoader(SettingsWindowController.class.getResource("Settings.fxml"));
AnchorPane page = (AnchorPane) fxmlLoader.load();