JavaFX:在运行时加载FXML并与控制器通信

时间:2016-10-16 16:56:22

标签: java javafx fxml

我在FXML文件中有一个容器(我们称之为主容器w / main控制器)并且我正在尝试生成其他FXML创建的节点并将它们作为子容器添加到主容器中。

我不认为我可以通过嵌套FXML来实现这一点,因为节点被添加到需要函数调用的自定义组件中。我以为我可以在主控制器的 initialize 中加载节点,但这会导致堆栈溢出,因为加载会递归调用initialize函数。

最好的方法是什么?我希望能够让主控制器响应并设置节点。我可以

主控制器看起来像:

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是自定义组件类,其中addCard添加了一个节点(CardLayout的实现)。

ImportPane的FXML现在几乎是空的。我只有一个针对根类的CSS样式,所以我可以看到它是如何布局的。

<?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>

主要组件的FXML是:

<?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>

1 个答案:

答案 0 :(得分:-1)

从FXML中删除 IIS manager --> Default Website --> handler Mapping 并使用FXMLLoader对象的fx:controller="phasiqplateanalyzer.PHASIQAnalyzerController"方法(没有静态方法调用...)

https://docs.oracle.com/javase/8/javafx/api/javafx/fxml/FXMLLoader.html#setController-java.lang.Object-

setController()
相关问题