简体   繁体   中英

open existing screen on button click

I am newbie 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)  

I don't understand how to open one screen on another screen.

Please give me reference or hint.

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();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM