1

I want to change the center of borderpane when I click a menu but it pops up an error "Root value already specified.". At first, I initialize it like below

@Override
    public void initialize(URL url, ResourceBundle rb) {
        // TODO
        currentMenuNum = 0;
        currentMenu = menuList[currentMenuNum];
        currentViewerPath = "/managerworld/viewer/StartViewer.fxml"; 
        System.out.println(currentMenu);
        setControlItems(currentViewerPath,currentMenu);

    }

private void setControlItems (String path, String currentMenu) {
        //Display setting for menu
        try {
            fxmlLoader.setLocation(getClass().getResource(path));
            anchorPane = fxmlLoader.load();
//            fxmlLoader.load();
            connectionViewerController = (ConnectionViewerController) fxmlLoader.getController();
        } catch (Exception e) {
            System.out.println(e.getStackTrace().toString());
            System.out.println(e.getMessage());
        }
        borderPane.setCenter(anchorPane);
        //controller setting
        if (currentMenu.equals(currentMenu)) connectionViewerController = (ConnectionViewerController) fxmlLoader.getController();
        else return;
        //button setting
        button2.setDisable(false);
        button3.setDisable(false);
        button4.setDisable(false);
        button5.setDisable(false);
    }

first time load is working well, but if I try to reload other fxml file, the error pops up.

@FXML
    private void handleSelectMenus(MouseEvent event) {
if (event.getSource().equals(menuVBox1)) {
            currentViewerPath = "/managerworld/viewer/ConnectionViewer.fxml";
            currentMenuNum = 1;
            currentMenu = menuList[currentMenuNum];
            setControlItems(currentViewerPath,currentMenu);
} else {
        }

    }

and my fxml files are like this..

 <AnchorPane prefHeight="200.0" prefWidth="200.0" BorderPane.alignment="CENTER">
         <children>
            <VBox alignment="CENTER" prefHeight="653.0" prefWidth="792.0" style="-fx-border-color: #D3D3D3;" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
               <children>
                  <Text fill="#656565" strokeType="OUTSIDE" strokeWidth="0.0" text="Welcome to Manager World!">
                     <font>
                        <Font size="36.0" />
                     </font>
                  </Text>
                  <Text fill="#003975" layoutX="41.0" layoutY="329.0" strokeType="OUTSIDE" strokeWidth="0.0" text="Click the Connection Menu to get started.">
                     <font>
                        <Font size="16.0" />
                     </font>
                  </Text>
               </children>
            </VBox>
         </children>
      </AnchorPane>

fxml which is including borderpane is like this..

<BorderPane fx:id="borderPane" maxHeight="768.0" maxWidth="1024.0" minHeight="768.0" minWidth="1024.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="managerworld.controller.ManagerWorldController">
          <center>
       </center>

and this is what I want to change if I click a menu

<AnchorPane xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="managerworld.controller.ConnectionViewerController">
         <children>
            <VBox spacing="5.0" style="-fx-border-color: #D3D3D3;" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
               <children>
                  <GridPane alignment="CENTER_LEFT" layoutX="100.0" layoutY="100.0" BorderPane.alignment="CENTER">

Would someone help me this problem?

James
  • 305
  • 8
  • 19
  • I solved this problem myself as I added "fxmlLoader.setRoot(null);" this before setLocation. Is it optimal way to solve this problem? – James Nov 22 '15 at 08:45
  • 2
    Create a new `FXMLLoader` each time, instead of reusing the same one. – James_D Nov 22 '15 at 14:21
  • Then, what is the difference between making fxmlloader be null and creating new one each time? – James Nov 22 '15 at 14:23
  • (I assume you mean the difference between setting the `root` of the `FXMLLoader` to `null`, and creating a new one.) The `FXMLLoader` has more state than just the `root` that can only be initialized once: eg the controller, etc. So I think even with setting the root to null, if you try to reuse the same `fxmlLoader` to load an FXML file with a `fx:controller` attribute, you will also get an error. Additionally, the `fxmlLoader` holds references to all the `fx:id` attributes, and you should probably make sure those are cleared out to avoid conflicts. It's much safer to create a new loader. – James_D Nov 22 '15 at 14:30
  • thanks a lot. so I should change my code like this. – James Nov 22 '15 at 14:35
  • try { FXMLLoader fxmlLoader = new FXMLLoader(); fxmlLoader.setLocation(getClass().getResource(path)); – James Nov 22 '15 at 14:35
  • Yes, that is the better way to do it. – James_D Nov 22 '15 at 14:45
  • Thank you for your clever answer. – James Nov 22 '15 at 14:47

0 Answers0