10

BorderPane(MainControllerに関連付けられている)があります。BorderPaneのFXMLは、BorderPane<fx:include>の下部領域にLabel(コントローラーStatusBarControllerを含む)を含めるために使用します。残念ながら、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:ラベルとそれに関連するコントローラー

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

StatusBarControllerのフィールドを持つMainController:

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

そして、アプリケーションの開始:

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();
    }

私のサンプルの完全なソースコードは、http://codestefan.googlecode.com/svn/trunk/SubcontrollerAccess/で入手できます。

したがって、問題は次のとおりです。StatusBarControllerがMainControllerのstatusbarController変数に挿入されないのはなぜですか。

ヒントをありがとう!

4

2 に答える 2

19

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

main.fxmlあなたの:を更新します

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

その後、次の定数を使用できますMainController.java

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

statusbarControllerこれは部分的に小文字のクラス名ではなく、fx:id+Controller単語であることに注意してください。

于 2013-02-09T23:34:32.530 に答える
0

ネストされたfxmlを使用するnetbeans8.0にもバグがあるようです。ネストされたfxmlのコントローラオブジェクトを作成するためにNetBeansを信頼することはできません。これは、MainControllerに手動で挿入する必要があります。コントローラがNetBeansで更新されるたびに、コントローラが消去されるため、面倒な作業になる可能性があります。この例では、

@FXMLプライベートStatusBarControllerstatusbarController;

この場合、手動でメインコントローラに接続すると、正常に機能します。大きなfxml/コントローラーを整理するのに非常に便利です。

于 2014-06-24T13:25:34.703 に答える