I have one container in an FXML file (let's call this the main container w/ main controller) and am trying to generate other FXML-created nodes and add them as children in the main container.
I don't think I can do this by nesting the FXML since the nodes are being added to a custom component that requires function calling. I thought I could load the nodes in the main controller's initialize but this leads to a stack overflow since the loading calls the initialize function recursively.
What's the best way to do this? I want to be able to have the main controller respond to and setup the nodes. I could
Main controller looks like:
public class MainController implements Initializable {
@FXML
private CardPane cardPane;
@Override
public void initialize(URL url, ResourceBundle rb) {
// setup panes
AnchorPane importPane = new AnchorPane();
FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("ImportPane.fxml"));
fxmlLoader.setRoot(importPane);
fxmlLoader.setController(this);
try {
fxmlLoader.load();
cardPane.addCard(importPane);
} catch (IOException exception) {
throw new RuntimeException(exception);
}
}
CardPane is the custom component class where addCard adds a node (an implementation of CardLayout).
The FXML for ImportPane is pretty much empty right now. I just have a CSS styling for the root class so I can see how it's laying out.
<?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.*?>
<?import java.net.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<fx:root id="AnchorPane" prefHeight="400.0" prefWidth="600.0" styleClass="mainFxmlClass" type="AnchorPane" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8.0.40">
<stylesheets>
<URL value="@view.css" />
</stylesheets>
<children>
<VBox alignment="CENTER" layoutX="121.0" layoutY="38.0" maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" styleClass="importPane" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0" />
</children>
</fx:root>
The FXML for the main component is:
<?xml version="1.0" encoding="UTF-8"?>
<?import cardPane.*?>
<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<AnchorPane id="AnchorPane" prefHeight="200" prefWidth="320" stylesheets="@PHASIQAnalyzerView.css" xmlns="http://javafx.com/javafx/8.0.40" xmlns:fx="http://javafx.com/fxml/1" fx:controller="phasiqplateanalyzer.PHASIQAnalyzerController">
<children>
<VBox alignment="CENTER" maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
<children>
<HBox id="buttonBar" alignment="CENTER" maxHeight="-Infinity" maxWidth="1.7976931348623157E308" minWidth="-Infinity" prefHeight="100.0" prefWidth="200.0" />
<CardPane id="mainPane" fx:id="cardPane" maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" VBox.vgrow="ALWAYS" />
</children>
</VBox>
</children>
</AnchorPane>
remove fx:controller="phasiqplateanalyzer.PHASIQAnalyzerController"
from the FXML and use the setController()
method of an FXMLLoader object (no static method call for that...)
FXMLLoader fxmlLoader =new FXMLLoader();
fxmlLoader.setController(yourControllerClass);
fxmlLoader.load("yourFxmlFile");
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.