I currently have a fxml page with multiple panels within it. One of these panels has an AnchorPane that I'm using to encase fxml code for a particular area. Within it, I want to be able to change the content of it with different fxml snippets based upon what option is selected (via a button onClick button, or other occurrence.). I'd also like these snippets of code to use the controller of the parent that they will be inserted into if possible.
What my question here is, if this is possible at all? I currently have one test snippet that when the following code is executed on a particular button press
<VBox alignment="CENTER" layoutX="15.0" layoutY="16.0" prefHeight="177.0" prefWidth="918.0" xmlns:fx="http://javafx.com/fxml" fx:controller="com.footballsim.GameScreenSelectPlayOverviewController">
<children>
<Label fx:id="playTypeChooseLabelText" text="Choose your play type" textAlignment="CENTER">
<font>
<Font size="28.0" />
</font>
<VBox.margin>
<Insets bottom="15.0" left="15.0" right="15.0" top="15.0" fx:id="x1" />
</VBox.margin>
</Label>
<HBox alignment="TOP_CENTER" prefHeight="100.0" prefWidth="200.0">
<children>
<Button fx:id="shortRunPlayButton" mnemonicParsing="false" onAction="#handleShortRunSelection" text="1 - Short Run" HBox.margin="$x1">
<font>
<Font size="16.0" fx:id="x2" />
</font>
</Button>
<Button fx:id="longRunPlayButton" font="$x2" mnemonicParsing="false" onAction="#handleLongRunSelection" text="2 - Long Run" HBox.margin="$x1" />
<Button fx:id="passPlayButton" font="$x2" mnemonicParsing="false" onAction="#handlePassSelection" text="3 - Pass" HBox.margin="$x1" />
<Button fx:id="kickPuntPlayButton" font="$x2" mnemonicParsing="false" onAction="#handleKickPuntSelection" text="4 - Kick/Punt" HBox.margin="$x1" />
</children>
</HBox>
</children>
</VBox>
Both a runtimeexception and reflect.InvocationTargetException occur at this line (highlighted in bold)
| | try { | |
| | | FXMLLoader loader = new FXMLLoader(GameScreen.class.getResource("view/test.fxml")); |
| | | VBox textAreaAnchorPaneObject = (VBox) loader.load(); |
| | | |
| | | textAreaAnchorPane.getChildren().clear(); |
| | | textAreaAnchorPane.getChildren().add(textAreaAnchorPaneObject); |
| | | AnchorPane.setRightAnchor(textAreaAnchorPaneObject, 0.0); |
| | | AnchorPane.setLeftAnchor(textAreaAnchorPaneObject, 0.0); |
| | | AnchorPane.setTopAnchor(textAreaAnchorPaneObject, 0.0); |
| | | AnchorPane.setBottomAnchor(textAreaAnchorPaneObject, 0.0); |
| | } catch (IOException e) { | |
| | | e.printStackTrace(); |
| | } | |
The xmlns and fx:controller lines were not originally part of the file, but with errors that occurred, their addition resolved them. I did find that pointing to the parent fxml's controller woudln't fix the issue, as it'd be invoked twice. Based on the errors I'm jsut getting furthur into a hole, so I'm wondering ifwhat I'm trying to do is possible, and if so, how it could be properly done.