Sorry I am new to JavaFX. My setup: I have one main FXML GUI which is displayed the entire time. Inside this main GUI I have loaded another FXML in an AnchorPane.
The second FXML is loaded into this AnchorPane (which is in the main GUI) initially using:
@FXML
private AnchorPane rootSetPane;
I then use the rootSetPane
from there.
This is the AnchorPane which is inside the same main GUI fxml.
<AnchorPane fx:id="rootSetPane" layoutX="228.0" prefHeight="710.0" prefWidth="887.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="228.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0" />
Initialize main controller (main GUI is displayed the entire time)
@Override
public void initialize(URL url, ResourceBundle rb) {
AnchorPane paneHome;
try {
paneHome = FXMLLoader.load(getClass().getResource("HomePage.fxml"));
rootSetPane.getChildren().setAll(paneHome);
} catch (IOException ex) {
Logger.getLogger(FXMLDocumentController.class.getName()).log(Level.SEVERE, null, ex);
}
}
An example of a working method in this main class (this is called from a button in the same main FXML):
@FXML
private void foodFinder(ActionEvent event) throws IOException {
AnchorPane paneFoodFinder = FXMLLoader.load(getClass().getResource("ScreenFoodFinder.fxml"));
rootSetPane.getChildren().setAll(paneFoodFinder);
}
An example of a method that is not working (this is called using an object):
public void setPanel() throws IOException {
AnchorPane newPane = FXMLLoader.load(getClass().getResource("NewPane.fxml"));
rootSetPane.getChildren().setAll(newPane);
}
I set this second pane several times during the program using buttons in the main GUI (this works). The problem occurs when attempting to set this pane from outside the main controller class. I can not use an instance of the Main class and call a method to access rootSetPane
(and I can't make the rootSetPane
static). I am getting a NullPointerException
. I am sure this is a amateur mistake. Is there a way I can change this rootSetPane
using a method like the ones above.
Or do I have to make other classes to manage my screens. The easiest (even dirty) solution would be appreciated.
It would also work if I were able to fire a button in the main GUI, but this is resulting in the same issue (NullPointerException
). I have a feeling that I can not reference the elements using an object of the class and I also can not make these elements static.