2016-10-16 33 views
0

FXMLファイルに1つのコンテナがあります(これをメインコントローラのメインコントローラと呼びましょう)、他のFXML作成ノードを生成して、主な容器。JavaFX:実行時にFXMLをロードし、コントローラと通信する

ノードが関数呼び出しを必要とするカスタムコンポーネントに追加されているので、FXMLをネストすることでこれを行うことはできません。私はメインコントローラのノードをロードすることができたと思ったを初期化しますが、これはロードが初期化関数を再帰的に呼び出すため、スタックオーバーフローにつながります。

これを行うにはどうすればよいですか?私はメインコントローラがノードに応答してセットアップされるようにしたい。

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

FXMLからfx:controller="phasiqplateanalyzer.PHASIQAnalyzerController"を除去しFXMLLoaderオブジェクトのsetController()方法を使用(そのためのない静的メソッド呼び出しを...)

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

FXMLLoader fxmlLoader =new FXMLLoader(); 
fxmlLoader.setController(yourControllerClass); 
fxmlLoader.load("yourFxmlFile"); 
+0

構文は少し異なりますが、彼の考えは正しい。 –

+0

新しい動作を初期化します: –

+0

'//設定ペイン FXMLLoader mainfxmlLoader = new FXMLLoader(getClass()。getResource(" PHASIQAnalyzerView.fxml ")); mainfxmlLoader.setController(this); AnchorPane importPane = new AnchorPane(); FXMLLoader fxmlLoader =新しいFXMLLoader(getClass()。getResource( "ImportPane.fxml")); fxmlLoader.setRoot(importPane); fxmlLoader.setController(this); try {...} –

関連する問題