提问者:小点点

如何将窗格从其他控制器加载到中心窗格中的 vbox 中


我有一个边框窗格,顶部窗格中带有按钮,可将视图加载到边框窗格的左窗格中,然后在加载左窗格时,一个按钮将视图加载到中央窗格中的vbox中

主.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);
    }
    
    }
    
}

然后是主控制器.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());
     }
    
}

布局1控制器.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());  

        }
    }
}

主.xml

<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>

布局1.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>

现在,当我从控制器1访问VBox,mainController中的中心窗格时,它给我一个nullpointer错误,所以请帮助我如何从控制器1将layout2加载到中心窗格


共1个答案

匿名用户

好的,终于可以让centerPane加载layout2.fxml了。所以我所要做的就是为centerPane创建get方法,当在ShowLayout1中创建Layout1Controller时,传递MainController

主控制器代码:

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());
     }
    
}

布局1控制器代码

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());  

        }
    }
}