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

问题描述

我在 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);
    }
    
    }
    
}

然后是 MainMainController.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 访问 VBox、mainController 中的 centerPane 时,它​​给了我一个空指针错误 因此,请提供有关如何将 layout2 从 Controller1 加载到 centerPane 中的任何帮助

解决方法

好的,最后它能够让 centerPane 加载 layout2.fxml。所以我所要做的就是在 centerPane

中创建 MainController 时为 Layout1Controller 传递 ShowLayout1 创建一个 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());  

        }
    }
}

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...