Subcontroller nicht injiziert werden, in den main-controller
Habe ich ein BorderPane (verbunden mit einem MainController), der FXML für die BorderPane verwendet <fx:include>
um ein Label (mit einem controller StatusBarController) in der unteren region des BorderPane. Leider ist das die StatusBarController ist nicht injiziert in die Instanz der Klasse MainController und ich kann nicht verstehen, warum.
main.fxml: BorderPane mit dem mitgelieferten statusbar
<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: Das Label und der zugehörige controller
<Label fx:id="statusbar" text="A label simulating a status bar" xmlns:fx="http://javafx.com/fxml" fx:controller="com.example.controllers.StatusBarController" />
Die MainController mit einem Feld für die 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
}
}
Und der start der Anwendung:
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();
}
Den vollständigen source-code von meinem Probe ist verfügbar bei http://codestefan.googlecode.com/svn/trunk/SubcontrollerAccess/
Also die Frage ist: Warum ist das StatusBarController nicht injiziert in das statusbarController variable der MainController?
Dank für jeden Hinweis!
Du musst angemeldet sein, um einen Kommentar abzugeben.
Verwenden
@FXML
tag haben Sie zu bietenfx:id
.Aktualisieren Sie Ihre
main.fxml
:Nach, dass Sie können neben Konstanten in Ihrem
MainController.java
:Beachten Sie, dass
statusbarController
ist nicht eine wenigstens teilweise klein geschrieben Klassennamen, aberfx:id
+Controller
Wort.Scheint es ein bug in netbeans 8.0 mit verschachtelten fxmls als gut. Kann nicht zählen, auf netbeans erstellen der verschachtelten fxml das controller-Objekt für Sie, es hat manuell eingefügt MainController. Jedes mal, wenn der controller aktualisiert in netbeans wird es ausgelöscht hat, so kann es sein Art von langweilig. Für dieses Beispiel wäre das einfügen der
@FXML
private StatusBarController statusbarController;
Zeile manuell in der main-controller in diesem Fall, dann funktioniert es normalerweise. Sehr nützlich für die Organisation von großen fxmls/Controller.