我有主应用程序类,可以很好地完成以下任务:
@Override
public void start(Stage primaryStage) {
try {
FXMLLoader loader = new FXMLLoader(getClass().getResource(
"RecordScreen.fxml"));
Parent root = (Parent) loader.load();
Scene newScene = new Scene(root);
Stage newStage = new Stage();
newStage.setScene(newScene);
newStage.show();
} catch (Exception e) {
e.printStackTrace();
}
}
它启动一个显示人的表格视图.我选择一个人,点击"编辑"按钮,然后尝试启动一个窗口,让我对其进行编辑。
@FXML
public void editPerson() {
try {
FXMLLoader loader = new FXMLLoader(getClass().getResource(
"PersonEditor.fxml"));
PersonEditorCtrl ctrl = loader.getController();
ctrl.init(table.getSelectionModel().getSelectedItem());
Parent root = (Parent) loader.load();
Scene newScene = new Scene(root);
Stage newStage = new Stage();
newStage.setScene(newScene);
newStage.show();
} catch (Exception e) {
e.printStackTrace();
}
}
问题是,getController返回null.在过去的两个星期中,我一直在遵循这种模式,没有任何问题.我现在在做什么错? 这些难以追踪的错误正在加剧!!!
这是我的两个fxml:
具有表格视图的屏幕:
<AnchorPane xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/2.2" fx:controller="application.RecordsCtrl">
<!-- TODO Add Nodes -->
<children>
<VBox id="VBox" alignment="CENTER" spacing="0.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
<children>
<TableView fx:id="table" prefHeight="-1.0" prefWidth="-1.0">
<columns>
<TableColumn prefWidth="75.0" text="Name" fx:id="nameCol" />
<TableColumn prefWidth="75.0" text="Age" fx:id="ageCol" />
</columns>
</TableView>
<Button mnemonicParsing="false" onAction="#editPerson" text="Edit" />
</children>
</VBox>
</children>
</AnchorPane>
人员编辑:
<AnchorPane xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/2.2" fx:controller="application.PersonEditorCtrl">
<!-- TODO Add Nodes -->
<children>
<VBox layoutX="0.0" layoutY="0.0" prefHeight="-1.0" prefWidth="-1.0">
<children>
<TextField fx:id="nameField" prefWidth="200.0" />
<TextField fx:id="ageField" prefWidth="200.0" />
<Button mnemonicParsing="false" text="Button" />
</children>
</VBox>
</children>
</AnchorPane>
最新回答
- 2021-1-111 #
相关问题
- java:javafx自动调整大小和按钮填充javajavafxfxml2021-01-11 01:58
- java:将JavaFX FXML对象分组在一起javajavafxjavafx2fxml2021-01-07 06:56
- java:Javafx 8:在initialize方法中填充TableViewjavajavafxfxml2020-12-26 16:56
- JavaFx,@ FXML问题javafxfxml2020-12-26 11:25
- java:JavaFX-如何正确配置这多个切换按钮(一组)操作以满足我的需要javajavafxfxmlscenebuilder2020-12-24 16:54
更改此
为此:
您首先必须运行
loader.load()
那么您可以获取控制器。帕特里克