簡體   English   中英

JavaFX + MVC。 更改BorderPane和NullPointerException中的中心

[英]JavaFX+MVC. Change center in BorderPane and NullPointerException

我有2個控制器和3個FXML文件。
1.控制器:
MainController.java
在此控制器中,我使用BorderPane創建舞台和場景。 在BorderPane的中心,我想更改布局(GridPane,HBox等)

public class MainController {

    //Main BorderPane
    private BorderPane borderPane;

    @FXML
    private void initialize() {
       try {

          FXMLLoader  mainLoaderFXML = new FXMLLoader();
          mainLoaderFXML.setLocation(getClass().getResource("BaseView.fxml"));
          borderPane = (BorderPane) mainLoaderFXML.load();

          Stage mainStage = new Stage();
          mainStage.setTitle("Border Pane");

          Scene mainScene = new Scene(borderPane);
          mainStage.setScene(mainScene);

          FXMLLoader loader = new FXMLLoader();
          loader.setLocation(getClass().getResource("CenterView1.fxml"));

          //BorderPane in CenterView1.fxml          
          BorderPane centerView1 = (BorderPane) loader.load();

          borderPane.setCenter(centerView1);

          mainStage.show();


       } catch (IOException e) {
          e.printStackTrace();
       }

    }

    //Get BorderPane
    public BorderPane getBorderPane() {
         return this.borderPane;
    }

}

SecondController.java
在SecondController中,我想使用幫助radioButton更改borderPane MainController的中心。

public class SecondController {
    @FXML
    private ToggleGroup radioButtons;

    @FXML
    private void initialize() {

        radioButtons.selectedToggleProperty().addListener(new ChangeListener<Toggle>() {
            public void changed(ObservableValue<? extends Toggle> ov,
                Toggle old_toggle, Toggle new_toggle) {
                if (radioButtons.getSelectedToggle() != null) {
                    RadioButton chk = (RadioButton)new_toggle.getToggleGroup().getSelectedToggle(); 

                    switch(chk.getText()){
                        case "rbFirst": 
                            System.out.println("Default View CenterView1"); 
                            break;
                        case "rbSecond": 

                            FXMLLoader  mainLoaderFXML = new FXMLLoader();
                                mainLoaderFXML.setLocation(getClass().getResource("BaseView.fxml"));

                                MainController mainController = (MainController)mainLoaderFXML.getController();

                                //And now i want to change new FXML-file into center, but i have a NULLPointerException
                                System.out.println(mainController.getDefaultNormsLayout().setCenter("CenterView2"));
                            break;
                    }
                }
            }
        });
    }
}


2. FXML文件:
0.BaseView.fxml-基本視圖
1. CenterView1.fxml (默認情況下)-它是Base View中心的BorderPane
2. CenterView2.fxml-它是基本視圖中心的DataGrid

當我單擊rbSecond時,我在mainController.getDefaultNormsLayout()中看到NullPointerException 我知道這是什么,但我不知道要修復它。 我正在使用JavaFX類控制器場景參考在此處輸入鏈接描述等,但我不知道如何將其應用於我的任務。
謝謝!

我的評論回答:

出現空指針是因為在FXMLLoader的加載操作之前未初始化控制器。 這將創建MainController類的另一個實例。 如果未覆蓋默認的JavaFX,則此實例將與初始實例不同。 請參見FXMLLoader類的setControllerFactory方法。 請記住,不要重用FXMLLoader類,這一點很重要。

以下內容應確保mainController不再為null。

FXMLLoader  mainLoaderFXML = new FXMLLoader(); 
mainLoaderFXML.setLocation(getClass().getResource("BaseView.fxml"));
mainLoaderFXML.load();
MainController mainController =  (MainController)mainLoaderFXML.getController();

單例控制器

使用您選擇的單例機制並獲取實例。 如果要控制控制器類的創建,請使用。

mainLoaderFxml.setControllerFactory(new Callback() {
     public Object call(Class<?> clazz) {
          return instance of your class;
     }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM