1

I am designing an javafx application using BorderPane as Rootlayout inside the border pane there is button on left. on Click of which i need to set the borderpane(rootlayout) center to a different addprodct.fxml . i want to add a vbox on Click on Insert Button inside Center of BorderPane.

I checked if my fxml is getting loaded properly or not with this System.out.println("view folder: " + RootController.class.getResource("/Views/AddProduct.fxml")); its print exact path to fxml. i tried to Setting Stage with the new fxml to see if it loads properly and it worked properly.

        FXMLLoader loader = new FXMLLoader();
        System.out.println("view folder: " + RootController.class.getResource("/Views/AddProduct.fxml"));
        loader.setLocation(RootController.class.getResource( "/Views/AddProduct.fxml"));
        tryvbox = loader.load();
        main.getPrimaryStage().setScene(new Scene(tryvbox));

Yes, i know same question already have been asked but i implemented the solution given neither helped my problem. Load new fxml in borderpane center

JavaFX: How to update the center view of a borderpane with new values

Main.java


    package sample;

    import Controllers.RootController;
    import javafx.application.Application;
    import javafx.fxml.FXML;
    import javafx.fxml.FXMLLoader;
    import javafx.scene.Scene;
    import javafx.scene.layout.BorderPane;
    import javafx.stage.Stage;
    import java.io.IOException;


    public class Main extends Application {
        @FXML
        private BorderPane rootLayout;
        private Stage  primaryStage;
        @Override
        public void start(Stage primaryStage) throws Exception{
            this.primaryStage = primaryStage;
            mainwindow();
        }
        public void mainwindow() throws IOException {
            FXMLLoader loader = new FXMLLoader();
            loader.setLocation(getClass().getResource("/Views/Rootlayout.fxml"));
            rootLayout = loader.load();

            RootController rootController = loader.getController();
            rootController.setMain(this);
            Scene scene= new Scene(rootLayout);
            primaryStage.setTitle("Inventory Manager");
            primaryStage.setScene(scene);
            primaryStage.show();
        }
        public BorderPane getRootLayout() {
            return rootLayout;
        }

        public Stage getPrimaryStage() {
            return primaryStage;
        }

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

RootController.java

package Controllers;

import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.control.Button;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.VBox;
import sample.Main;

import java.io.IOException;

public class RootController {
    @FXML
    private Button rootBtnInsrt;
    @FXML
    private Button rootBtnUpdate;
    @FXML
    private BorderPane rootLayout;
    private Main main;

    public void setMain(Main main) {
        this.main = main;
    }

    @FXML
    void btnInsrtClick(MouseEvent event) {
        FXMLLoader loader = new FXMLLoader();
        loader.setLocation(RootController.class.getResource( "/Views/AddProduct.fxml"));
        try {
            VBox addProduct1 = loader.load();
        } catch (IOException e) {
            e.printStackTrace();
        }

        AddProductController addProductController= loader.getController();
        addProductController.setMain(main);
        main.getRootLayout().setCenter(rootLayout);
    }
}

rootlayout.fxml

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Menu?>
<?import javafx.scene.control.MenuBar?>
<?import javafx.scene.control.MenuItem?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.BorderPane?>
<?import javafx.scene.layout.VBox?>

<BorderPane fx:id="rootLayout" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="Controllers.RootController">
   <top>
      <MenuBar prefHeight="21.0" prefWidth="600.0" BorderPane.alignment="CENTER">
        <menus>
          <Menu mnemonicParsing="false" text="File">
            <items>
              <MenuItem mnemonicParsing="false" text="Close" />
            </items>
          </Menu>
          <Menu mnemonicParsing="false" text="Edit">
            <items>
              <MenuItem mnemonicParsing="false" text="Delete" />
            </items>
          </Menu>
          <Menu mnemonicParsing="false" text="Help">
            <items>
              <MenuItem mnemonicParsing="false" text="About" />
            </items>
          </Menu>
        </menus>
      </MenuBar>
   </top>
   <bottom>
      <AnchorPane prefHeight="29.0" prefWidth="600.0" style="-fx-background-color: Black;" BorderPane.alignment="CENTER" />
   </bottom>
   <left>
      <VBox alignment="CENTER" prefHeight="345.0" prefWidth="164.0" spacing="20.0" BorderPane.alignment="CENTER">
         <children>
            <Button fx:id="rootBtnInsrt" mnemonicParsing="false" onMouseClicked="#btnInsrtClick" prefHeight="50.0" prefWidth="132.0" text="Insert " />
            <Button fx:id="rootBtnUpdate" mnemonicParsing="false" onMouseClicked="#btnUpdateClick" prefHeight="50.0" prefWidth="135.0" text="Update" />
            <Button mnemonicParsing="false" prefHeight="50.0" prefWidth="158.0" text="View" />
         </children>
         <padding>
            <Insets left="10.0" right="10.0" />
         </padding>
      </VBox>
   </left>
</BorderPane>

Addproduct.fxml

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.ColumnConstraints?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.layout.RowConstraints?>
<?import javafx.scene.layout.VBox?>

<VBox fx:id="addproduct" alignment="CENTER" prefHeight="249.0" prefWidth="400.0" xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1">
   <children>
      <Label alignment="CENTER" text="Add Product" VBox.vgrow="ALWAYS" />
      <GridPane VBox.vgrow="ALWAYS">
         <columnConstraints>
            <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" />
            <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" />
         </columnConstraints>
         <rowConstraints>
            <RowConstraints minHeight="10.0" vgrow="SOMETIMES" />
            <RowConstraints minHeight="10.0" vgrow="SOMETIMES" />
            <RowConstraints minHeight="10.0" vgrow="SOMETIMES" />
            <RowConstraints minHeight="10.0" vgrow="SOMETIMES" />
         </rowConstraints>
         <children>
            <Label text="Product Code" />
            <Label text="Product Name :" GridPane.rowIndex="1" />
            <Label text="Product Price :" GridPane.rowIndex="2" />
            <Label text="Product Quantity :" GridPane.rowIndex="3" />
            <TextField GridPane.columnIndex="1" />
            <TextField GridPane.columnIndex="1" GridPane.rowIndex="1" />
            <TextField GridPane.columnIndex="1" GridPane.rowIndex="2" />
            <TextField GridPane.columnIndex="1" GridPane.rowIndex="3" />
         </children>
         <padding>
            <Insets bottom="20.0" left="20.0" right="20.0" top="20.0" />
         </padding>
      </GridPane>
      <HBox alignment="CENTER" spacing="30.0">
         <children>
          <Button alignment="CENTER" maxHeight="40.0" maxWidth="1.7976931348623157E308" mnemonicParsing="false" text="ADD" HBox.hgrow="ALWAYS" />
            <Button alignment="CENTER" maxHeight="40.0" maxWidth="1.7976931348623157E308" mnemonicParsing="false" text="Reset" HBox.hgrow="ALWAYS" />
            <Button maxHeight="40.0" maxWidth="1.7976931348623157E308" mnemonicParsing="false" text="Back" HBox.hgrow="ALWAYS" />
         </children>
         <padding>
            <Insets bottom="20.0" left="20.0" right="20.0" />
         </padding>
      </HBox>
   </children>
</VBox>

when tried running the following code i get error.

  1. Exception in thread "JavaFX Application Thread" java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
  2. Caused by: java.lang.reflect.InvocationTargetException
  3. Caused by: java.lang.NullPointerException

all in same error pane this is my directory structure. Directory Structure

DarkSource
  • 11
  • 4
  • sorry that code for root controller had been posted twice. but it had fx:id assign too – DarkSource Jul 17 '19 at 11:31
  • Possible duplicate of [How to reference javafx fxml files in resource folder?](https://stackoverflow.com/questions/19602727/how-to-reference-javafx-fxml-files-in-resource-folder) – Matt Jul 17 '19 at 13:40
  • Also check out https://stackoverflow.com/questions/53699116/cant-load-fxml-in-another-package-javafx – Matt Jul 17 '19 at 13:42
  • i checked the if files are loded or not using System.out.println("view folder: " + RootController.class.getResource("/Views/AddProduct.fxml")); which prints as follows: view folder: file:/C:/Users/***/IdeaProjects/Management/out/production/Management/Views/AddProduct.fxml also converted my Views folder to resources but neither worked – DarkSource Jul 17 '19 at 16:07
  • is my directory structure a problem or is it my code. i tried putting my Views folder in /src/sample/views but that gave me a diffrent error. – DarkSource Jul 17 '19 at 16:12
  • i think it throws error on this line rootcontroller.java line 32: VBox addProduct = loader.load(); – DarkSource Jul 17 '19 at 16:15
  • Read the linked posts the reason you are getting an error is because your project cannot load the FXML(Because it can't find it) due to your directory structure once you change that reflect the changes in the code `getResource(...)` checks in the you guessed it the resource folder it doesn't search through all directories until is finds your fxml – Matt Jul 17 '19 at 16:19
  • i tried setting stage with the new fxml it was working fine with this the new fxml opened :- FXMLLoader loader = new FXMLLoader(); loader.setLocation(RootController.class.getResource( "/Views/AddProduct.fxml")); tryvbox = loader.load(); main.getPrimaryStage().setScene(new Scene(tryvbox)); – DarkSource Jul 17 '19 at 16:21
  • my fxml was getting loaded – DarkSource Jul 17 '19 at 16:23

1 Answers1

0

So basically after all the work and help the problem arose due to the Try/Catch block.

FXMLLoader loader = new FXMLLoader();
loader.setLocation(RootController.class.getResource( "/Views/AddProduct.fxml"));
    try {
        VBox addProduct1 = loader.load();
    } 
    catch (IOException e) {
        e.printStackTrace();
    }

i removed the try/catch and added exception to the method i.e void btnInsrtClick(MouseEvent event) throws IOException

DarkSource
  • 11
  • 4