如何将来自不同控制器的窗格加载到中心窗格中的 vbox

how to load a pane from a different controller into a vbox in the center pane

我在 TopPane 中有一个带按钮的边框,用于将视图加载到边框的左窗格中,然后在加载左窗格时使用一个按钮将视图加载到中心窗格中的 vbox

Main.java

public class Main extends Application {
    @Override
    public void start(Stage stage) throws Exception{
        //this is to load the parent (borderpane)

        FXMLLoader loader = new FXMLLoader(getClass().getResource("/Views/fxml/main.fxml"));
        
        //this is to create a controller for the parent view

        loader.setController(new MainController());
        
        Parent root = loader.load();
        
        Scene scene = new Scene(root);
        stage.setScene(scene);
        stage.show;
        
        /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        launch(args);
    }
    
    }
    
}

然后是主MainController.java

public class MainController implements Initializable {
    
    @FXML
    protected BorderPane borderPane;

    // This is a VBox occupying the Centerpane of the Borderpane

    @FXML
    protected VBox centerPane;

public MainController(){         
         }
     
    @Override
    public void initialize(URL url, ResourceBundle rb) {
        
                }    

// This loads the layout1 into the left pane of the borderpane from button handler inside the Top pane

    @FXML
    private void showLayout1(ActionEvent event) throws IOException {
        FXMLLoader loader = new FXMLLoader();
        loader.setLocation(getClass().getResource( "/Views/fxml/Layout1.fxml"));
        loader.setController(new Controller1());
        leftPane.getChildren().clear();
        leftPane.getChildren().add(loader.load());
     }
    
}

Layout1Controller.java

public class Layout1Controller implements Initializable{
    private MainController app;
    
    @Override
    public void initialize(URL url, ResourceBundle rb){        
    }
    
    @FXML
    private void ShowLayout2(ActionEvent event) throws IOException{
        FXMLLoader loader2 = new FXMLLoader();
        loader2.setLocation(getClass().getResource("/Views/fxml/Layout2.fxml"));
       loader2.setController(new Controller2());

// This is returning a null causing a java.lang.NullPointerException
           app.centerPane.getChildren().clear();      
           app.centerPane.getChildren().add(loader2.load());  

        }
    }
}

Main.fxml

<BorderPane fx:id="border_pane" prefHeight="650.0" prefWidth="1018.0" style=" type="BorderPane" xmlns="http://javafx.com/javafx/8.0.141" xmlns:fx="http://javafx.com/fxml/1">
   <left>
      <VBox fx:id="leftPane" alignment="CENTER" prefHeight="569.0" prefWidth="215.0"  />
   </left>
   <center>
      <VBox fx:id="centerPane" alignment="CENTER" prefHeight="586.0" prefWidth="701.0"  BorderPane.alignment="CENTER">
      </VBox>
   </center>
   <top>
      <HBox prefHeight="81.0" prefWidth="876.0" >
<children>
     <Button fx:id="btnSGRE" mnemonicParsing="false" onAction="#ShowLayout1" prefHeight="26.0" prefWidth="151.0" style="-fx-background-color: transparent;" text="REVENUE ENTRIES" textFill="#11124a">
  <font>
     <Font name="Century" size="13.0" />
    </font>
         </Button>
     </children>
        </HBox>
   </top>
</BorderPane>

Layout1.fxml

<AnchorPane prefHeight="565.0" prefWidth="215.0"  xmlns="http://javafx.com/javafx/8.0.171" xmlns:fx="http://javafx.com/fxml/1" >
   <children>
      <VBox alignment="BASELINE_CENTER" layoutY="40.0" prefHeight="415.0" prefWidth="215.0" >
         <children>
            <Text strokeType="OUTSIDE" strokeWidth="0.0" text="Revenue Entries">
               <font>
                  <Font name="System Bold" size="18.0" />
               </font>
            </Text>
            <HBox alignment="BASELINE_CENTER" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="35.0" prefWidth="173.0" >
               <children>
                  <ComboBox fx:id="cmbRevGroup" onAction="#setCenters" prefHeight="35.0" prefWidth="195.0" promptText="Main Revenue Centers"/>
               </children>
            </HBox>
            <HBox alignment="BASELINE_CENTER" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="35.0" prefWidth="173.0">
               <children>
                  <ComboBox fx:id="cmbRevCent" onAction="#SelectedCenter" prefHeight="35.0" prefWidth="195.0" promptText="Revenue Centers"/>
     <Button  mnemonicParsing="false" onAction="#ShowLayout2" prefHeight="43.0" prefWidth="142.0"  text="Revenue Entries" />
               </children>
            </HBox>
         </children>
      </VBox>
</AnchorPane>

现在,当我从 Controller1 访问 mainController 中的 VBox、centerPane 时,它​​给我一个空指针错误 因此,请提供有关如何从 Controller1

将 layout2 加载到 centerPane 的任何帮助

好的,终于可以让 centerPane 加载 layout2.fxml。所以我所要做的就是在 ShowLayout1

中创建 Layout1Controller 时为 centerPane 传递 MainController 创建一个 get 方法

主控制器代码:

public class MainController implements Initializable {
    
@FXML
protected BorderPane borderPane;
    // This is a VBox occupying the Centerpane of the Borderpane

@FXML
private VBox centerPane;
 
public VBox getCenterPane(){
         return centerPane;
     }

public MainController(){         
         }
     
    @Override
    public void initialize(URL url, ResourceBundle rb) {
        
                }    

// This loads the layout1 into the left pane of the borderpane from button handler inside the Top pane

    @FXML
    private void showLayout1(ActionEvent event) throws IOException {
        FXMLLoader loader = new FXMLLoader();
        loader.setLocation(getClass().getResource( "/Views/fxml/Layout1.fxml"));
        loader.setController(new Controller1());
        Layout1Controller controller = (Layout1Controller)loader.getController();
        controller.setMainController(this);
        leftPane.getChildren().clear();
        leftPane.getChildren().add(loader.load());
     }
    
}

Layout1Controller code:

public class Layout1Controller implements Initializable{
    private MainController app;
    public void setMainController(MainController app){
         this.app = app;
     }
    @Override
    public void initialize(URL url, ResourceBundle rb){        
    }
    
    @FXML
    private void showLayout2(ActionEvent event) throws IOException{
        FXMLLoader loader2 = new FXMLLoader();
        loader2.setLocation(getClass().getResource("/Views/fxml/Layout2.fxml"));
       loader2.setController(new Controller2());

// This is returning a null causing a java.lang.NullPointerException
           app.getCenterPane().getChildren().clear();      
           app.getCenterPane().getChildren().add(loader2.load());  

        }
    }
}