Reputation: 1298
I have looked at this, this and this but sadly there is not a single one that resolves my problem. The components are always null. The ids are the same but I still don't know what I am doing wrong. Below is my code :
Application Class.
public class Main extends Application {
@Override
public void start(Stage primaryStage) throws Exception {
Parent root = FXMLLoader.load(LocalFileUtils.getResourceAsURL("RootView.fxml"));
primaryStage.setScene(new Scene(root));
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
RootView.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.shape.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.ColumnConstraints?>
<?import javafx.scene.layout.RowConstraints?>
<BorderPane id="mainPane" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="main.MainViewController">
<center>
<AnchorPane id="mainAnchorPane" prefHeight="200.0" prefWidth="200.0" BorderPane.alignment="CENTER" />
</center>
</BorderPane>
Controller Class
package main;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.layout.AnchorPane;
public class MainViewController implements Initializable {
@FXML
private AnchorPane mainAnchorPane;
@Override
public void initialize(URL location, ResourceBundle resources) {
// TODO Auto-generated method stub
System.out.println("Started");
System.out.println(location); // Prints out he URL of the RootView
System.out.println(resources); //Always Null
System.out.println(mainAnchorPane); //Always Null
}
}
The App runs okay. Have I missed something?
Upvotes: 1
Views: 806
Reputation: 1298
I modified the fxml to what @jonx suggested and everything works. below is the new FXML.
<AnchorPane fx:id="mainAnchorPane" prefHeight="200.0" prefWidth="200.0" BorderPane.alignment="CENTER" />
Upvotes: 0
Reputation: 787
In FXML you need to define ids different. the id in FXML must be like this
Fx:id="mainAnchorPane"
Upvotes: 2