2013-02-09 12 views
8

BorderPane(MainControllerに関連付けられている)があり、BorderPaneのFXMLには<fx:include>が使用され、Label(コントローラStatusBarController付き)をBorderPaneの下部領域に含めます。残念ながら、StatusBarControllerはMainControllerクラスのインスタンスには注入されません。理由を理解できません。サブコントローラがメインコントローラに注入されていない

main.fxml:付属のステータスバーとBorderPane

<fx:root type="javafx.scene.layout.BorderPane" fx:id="borderPane" xmlns:fx="http://javafx.com/fxml" fx:controller="com.example.controllers.MainController"> 
    <bottom> 
    <fx:include source="statusbar.fxml" /> 
    </bottom> 
</fx:root> 

statusbar.fxml:StatusBarControllerのためのフィールドとラベルとそれが関連付けられていますコントローラ

<Label fx:id="statusbar" text="A label simulating a status bar" xmlns:fx="http://javafx.com/fxml" fx:controller="com.example.controllers.StatusBarController" /> 

メインコントローラ:

public class MainController 
{ 
    @FXML 
    private StatusBarController statusbarController; // PROBLEM HERE: I would expect the StatusBarController to be injected but this does not happen! 


    // Implementing Initializable Interface no longer required on JavaFX 2.2 according to 
    // http://docs.oracle.com/javafx/2/fxml_get_started/whats_new2.htm 
    // (I also tested this, the initialize() method is being called) 
    @SuppressWarnings("unused") // only called by the FXMLLoader 
    @FXML // This method is called by the FXMLLoader when initialization is complete 
    private void initialize() { 
     // initialize your logic here: all @FXML variables will have been injected 
     assert borderPane != null : "fx:id=\"borderPane\" was not injected: check your FXML file 'main.fxml'."; 
     System.out.println("MainController initialize()"); 

     //statusbarController.setStatusText("Hello from MainController"); // PROBLEM HERE: this fails because statusbarController is not injected as expected 
    } 
} 

とアプリケーションの開始:なぜStatusBarControllerはstatusbarController変数に注入されていません。私のサンプルの完全なソースコードは、そこで質問があるhttp://codestefan.googlecode.com/svn/trunk/SubcontrollerAccess/

で利用可能である

public void start(Stage primaryStage) 
    {  
     Parent root = null; 

     try { 
      root = FXMLLoader.load(getClass().getResource("/resources/main.fxml")); 
     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 

     primaryStage.setTitle("Demo"); 
     primaryStage.setScene(new Scene(root, 800, 600)); 
     primaryStage.show(); 
    } 

MainControllerの?

ありがとうございました!

答えて

15

@FXMLタグを使用するには、fx:idを入力する必要があります。

更新しますmain.fxml:その後

<bottom> 
    <fx:include fx:id="statusbar" source="statusbar.fxml" /> 
</bottom> 

あなたはMainController.javaの次の定数を使用することができます:statusbarControllerはpartialy小文字のクラス名ではないことを、

@FXML 
Label statusbar; // node itself 
@FXML 
private StatusBarController statusbarController; // controller 

ますが、fx:id + Controller単語。

+1

私は、コントローラの変数の命名スキームを知っていたが、私はFXを追加逃した、ありがとう:直接FXへのidを:(bbviouslyこれはまた、具体的なIDから一般FXMLコードを分離することは理にかなって)含まれています。これは現在動作します:-) –

+0

@Sergey、私は同じ問題を抱えていました。私の問題はあなたのNOTE fx:id + Controllerにありました。いいキャッチ!私の問題を修正したので、この質問にあなたの手紙で答えてください。 https:// stackoverflow。com/questions/45475209/null-exception-when-i-try-to-access-a-controller-i-passed-javafx – Moe

+0

@Moe確かに、ヒントのために感謝:) –

0

ネストされたfxmlsとともにnetbeans 8.0にバグがあるようです。ネストされたfxmlのコントローラオブジェクトを作成するためにnetbeansをカウントすることはできません。手動でMainControllerに挿入する必要があります。コントローラがネットビーンで更新されるたびに、それは一掃され、退屈なものになることがあります。これを挿入するこの例では、

@FXML private StatusBarController statusbarController;

この場合、メインコントローラに手動で回線を設定すると、正常に動作します。大きなfxmls /コントローラの編成に非常に便利です。

関連する問題