FXML parent element not injected in child controller

94 Views Asked by At

I am trying to figure-out why parent element is always null in following JavaFX code. MenuBar is used as a child inside gl.fmxl (see above). MenuBar is using controller called MenuController. Now when I hit MenuController by clicking on a menu then @FXML protected VBox gl; is always null in GlController

here is how MenuBar component is created using menu_bar.fxml file below

public class MenuBar extends FlowPane {

    public MenuBar(){
        FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("fragments/top_menu.fxml"));
        fxmlLoader.setRoot(this);
        fxmlLoader.setController(new MenuController());
        try {
            fxmlLoader.load();
        } catch (IOException exception) {
            throw new RuntimeException(exception);
        }
    }
}

gl.fxml

<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.geometry.*?>
<?import javafx.scene.layout.*?>
<?import com.ui.layouts.MenuBar?>
<?import javafx.scene.control.Button?>
<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1"
            fx:controller="com.ui.controllers.GlController">

    <VBox fx:id="gl">
        <MenuBar fx:id="menuBar"></MenuBar>
    </VBox>
</AnchorPane>

top_menu.fxml

<?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.control.MenuBar?>
<?import javafx.scene.control.Menu?>
<?import javafx.scene.control.MenuItem?>
<?import javafx.scene.layout.FlowPane?>

<fx:root type="javafx.scene.layout.FlowPane" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity"
          minWidth="-Infinity" xmlns:fx="http://javafx.com/fxml">
   <children>
       <MenuBar >
           <menus>
               <Menu mnemonicParsing="false" text="File">
                   <items>
                       <MenuItem fx:id="createCsvFile" mnemonicParsing="false" onAction="#exportCsvData" text="GL" />
                   </items>
               </Menu>
               <Menu mnemonicParsing="false" text="CSV">
                   <items>
                       <MenuItem fx:id="openCsvFile" mnemonicParsing="false" onAction="#processCsvData" text="open" />
                   </items>
               </Menu>
               <Menu mnemonicParsing="false" text="Products">
                   <items>
                       <MenuItem mnemonicParsing="false" onAction="#showProducts" text="saving" />
                   </items>
               </Menu>
               <Menu mnemonicParsing="false" text="Accounts">
                   <items>
                       <MenuItem mnemonicParsing="false" onAction="#showAccountTransactions" text="show" />
                   </items>
               </Menu>
           </menus>
       </MenuBar>
   </children>
</fx:root>

Main.java

Here is how I initialise the javaFX application

public class Main extends Application {

    @Override
    public void start(Stage primaryStage) throws Exception{
      FXMLLoader fxmlLoader = new FXMLLoader();
    Parent parentView  = fxmlLoader.load(getClass().getResource("ui/layouts/gl.fxml"));
    primaryStage.setScene(new Scene(parentView, 1200, 800));
    primaryStage.show();

    }


    public static void main(String[] args) {
        launch(args);
    }
}

MenuController.java

public class MenuController {

    GlController glController = new GlController();

    @FXML protected void processCsvData(ActionEvent event) throws IOException {

        MenuItem menuItem = (MenuItem)event.getSource();
        if (menuItem.getId().equals(openCsvFile.getId())) {
            File file = browseFile("CSV", "*.csv");
            String csvData = buildCsv(file.getPath()).toString();
            List<Transactionable> transactionList = glToCsv.buildTransactions(csvData, TransactionType.ACTUAL_TRANSACTION);
            addTableItem(transactionTableView, transactionList);
        }
    }

}

GlController

public class GlController {

    @FXML protected VBox gl;

    public Scene getScene(){
        return gl.getScene();
    }
}

I am hitting processCsvData by clicking on menu to debug the value of glController.getScene() which is always null

0

There are 0 best solutions below