I am a beginner on JavaFX. I need to open another screen on button click which already created. For this, I try to search but didn't get anything helpful because every tutorial which I find are redirecting to create new screen. But I want to open screen which already created.
For this, I write following code in controller.java
@FXML
public void handleNewTestBedButtonAction(ActionEvent event) throws IOException {
URL url = getClass().getResource("com/sobc/testbed/gui/testbedform.fxml");
FXMLLoader fxmlloader = new FXMLLoader();
fxmlloader.setLocation(url);
fxmlloader.setBuilderFactory(new JavaFXBuilderFactory());
AnchorPane pane = new AnchorPane();
pane.getChildren().clear();
pane.getChildren().add((Node) fxmlloader.load(url.openStream()));
// here we go
//((SOARiteController) fxmlloader.getController()).setContext();
}
fxml code
<Button id="newtestbedbtn" fx:id="newTestBedButtonId" mnemonicParsing="false" onAction="#handleNewTestBedButtonAction" onMouseEntered="#NewTestBedButtonMouseEntered" onMouseExited="#NewTestBedButtonMouseExited" styleClass="imgbtn" text="" wrapText="false">
But I am getting following exception
Caused by: java.lang.NullPointerException
at com.soab.SOARiteController.handleNewTestBedButtonAction(SOARiteController.java:52)
How can I open one screen on another screen?
Use following code in replacement of your Fxml loader, this is a correct way to load fxml file.
URL url = getClass().getResource("com/sobc/testbed/gui/testbedform.fxml");
FXMLLoader loader = new FXMLLoader(url);
Node node = (Node) loader.load();