I'll try to get in JavaFX 2 and used a simple demo app. The Project consists of 3 Files, the Main.java, the Controller.java and the sample.fxml.
In Sample.fxml i declared the controller:
<GridPane fx:controller="sample.Controller"
xmlns:fx="http://javafx.com/fxml" alignment="center" hgap="10" vgap="10">
</GridPane>
And in my Main.java I try to access the controller
FXMLLoader loader = new FXMLLoader();
Parent root = loader.load(getClass().getResource("sample.fxml"));
System.out.println(loader.getController()); //prints always null
So my first idea was that the mapping doesn't work. So I added a initialize method in the controller.
@Override
public void initialize(URL url, ResourceBundle resourceBundle) {
System.out.println("init");
}
The output is now:
init
null
So my Question now is how can i access the controller of a given fxml file?