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の?
ありがとうございました!
私は、コントローラの変数の命名スキームを知っていたが、私はFXを追加逃した、ありがとう:直接FXへのidを:(bbviouslyこれはまた、具体的なIDから一般FXMLコードを分離することは理にかなって)含まれています。これは現在動作します:-) –
@Sergey、私は同じ問題を抱えていました。私の問題はあなたのNOTE fx:id + Controllerにありました。いいキャッチ!私の問題を修正したので、この質問にあなたの手紙で答えてください。 https:// stackoverflow。com/questions/45475209/null-exception-when-i-try-to-access-a-controller-i-passed-javafx – Moe
@Moe確かに、ヒントのために感謝:) –