1

I'm creating project in javafx for my last year project as a newbie in javafx I got confuse on few thing I have many doubt about loading fxml into parent. one doubt in them is

  @FXML
  private StackPane stackmain;

  public void start(Stage primaryStage) {
    try {
        Parent root =FXMLLoader.load(getClass().getResource("/application/Main.fxml"));
        Scene scene = new Scene(root);
        scene.getStylesheets().add(getClass().getResource("/application/application.css").toExternalForm());
        primaryStage.setScene(scene);
        primaryStage.show();
    } catch(Exception e) {
        e.printStackTrace();
    }
}

@FXML
public void newpopup(ActionEvent event) throws IOException {

     FXMLLoader fxmlLoader = new FXMLLoader();
        fxmlLoader.load(getClass().getResource("/anyfile.fxml").openStream());
        StackPane root1 = fxmlLoader.getRoot();
        stackMain.getChildren().clear();
        stackMain.getChildren().add(root1);
}

this is my maincontroller java file and i m fxml

  <StackPane fx:id="stackcountry" maxHeight="-Infinity"
             maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="598.0" 
             prefWidth="537.0" xmlns="http://javafx.com/javafx/8" 
             xmlns:fx="http://javafx.com/fxml/1" fx:controller="maincontroller">
    <children>
      <AnchorPane prefHeight="200.0" prefWidth="200.0">
         <children>
           <BorderPane layoutX="124.0" layoutY="83.0" prefHeight="598.0" 
            prefWidth="537.0" AnchorPane.bottomAnchor="0.0" 
            AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" 
            AnchorPane.topAnchor="0.0">
               <left>
                 <VBox prefHeight="598.0" prefWidth="138.0" 
                    BorderPane.alignment="CENTER">
                   <children>
                     <Button mnemonicParsing="false" onAction="#newpopup" 
                      prefHeight="44.0" prefWidth="138.0" text="newpopup" />
                   </children>
                </VBox>
              </left>
                <center>
                   <StackPane fx:id="stackmain" prefHeight="150.0" 
                    prefWidth="200.0" BorderPane.alignment="CENTER" />
                </center>
            </BorderPane>
          </children>
        </AnchorPane>
      </children>
    </StackPane>

now when user click on popupnew button then newpopup event get fire it call(merge) new.fxml into stackmain

<StackPane fx:id="stacknext" maxHeight="-Infinity" maxWidth="- Infinity" minHeight="-Infinity" minWidth="-Infinity"  prefHeight="598.0" prefWidth="537.0"  xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="maincontroller">
  <children>
    <AnchorPane prefHeight="200.0" prefWidth="200.0">
      <children>
        <Button onAction="#loadnewfxml" layoutX="113.0" layoutY="29.0" 
         mnemonicParsing="false" 
         prefHeight="36.0" prefWidth="111.0" text="open" />
      </children>
    </AnchorPane>
  </children>
</StackPane>

in my newcontroller I have one button when user click on that button another fxml got loaded

   @FXML
   private StackPane stacknext;
   @FXML
   public void newpopup(ActionEvent event) throws IOException {

    FXMLLoader fxmlLoader = new FXMLLoader();
    fxmlLoader.load(getClass().getResource("another.fxml").openStream());
    StackPane root1 = fxmlLoader.getRoot();

now here is my question why this code which describe below work when i use stacknext

    stacknext.getChildren().clear();
    stacknext.getChildren().add(root1);

but it would not work when use stackmain is this limitation of javafx or any other approach to I got confuse why this stackmain is not accessable from another.fxml

    stackmain.getChildren().clear();
    stackmain.getChildren().add(root1);

last thing I want to know what .getparent does anybody know please help me on this question

  • You aren't even using a controller with the main fxml. This should yield an error when you load the fxml since there is no way the event handler methods could be found without a controller... – fabian Aug 04 '17 at 11:34
  • @fabian i had add controller at main fxml can you clear my doubt –  Aug 04 '17 at 12:14
  • How are you passing the `stackmain` reference from one controller to the other controller? – James_D Aug 04 '17 at 13:13
  • @James_D that thing i m asking how i can refrenece to stackmain, I m using stackmain.getchild().add("fxmlLoader.load(getClass().getResource("another.fxml")); it throw error –  Aug 08 '17 at 05:52

0 Answers0