首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >FXMLLoader getController是否返回NULL?

FXMLLoader getController是否返回NULL?
EN

Stack Overflow用户
提问于 2014-05-05 04:21:33
回答 1查看 41.1K关注 0票数 22

我有一个主要的应用程序类,它可以很好地完成以下工作:

代码语言:javascript
复制
   @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();
      }
   }

它将启动一个显示人员的表视图。我选择一个人,点击编辑按钮,并尝试启动一个窗口,使我可以编辑他们。

代码语言:javascript
复制
   @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。在过去的两周里,我一直在遵循这个模式,没有任何问题。我现在做错了什么?这些无法追踪的bug正在恶化!

下面是我的两个fxml:

具有tableview的屏幕:

代码语言:javascript
复制
<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>

person编辑器:

代码语言:javascript
复制
<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>
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-05-05 04:34:29

改变这一点

代码语言:javascript
复制
@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();
      }
   }

关于这个问题:

代码语言:javascript
复制
@FXML
   public void editPerson() {
      try {
         FXMLLoader loader = new FXMLLoader(getClass().getResource(
               "PersonEditor.fxml"));
         Parent root = (Parent) loader.load();
         PersonEditorCtrl ctrl = loader.getController();
         ctrl.init(table.getSelectionModel().getSelectedItem());

         Scene newScene = new Scene(root);
         Stage newStage = new Stage();
         newStage.setScene(newScene);
         newStage.show();
      } catch (Exception e) {
         e.printStackTrace();
      }
   }

您首先必须运行loader.load(),然后才能获得控制器。

帕特里克

票数 60
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23461148

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档