简体   繁体   中英

How do I use FXMLLoader inside EventHandler?

I am creating a ListView with the ability to double click each item and have a window popup with inputs structured by an FXML file. The FXML file itemStep contains fx:controller="controller.ItemStep"

listViewVariable.setOnMouseClicked(new EventHandler<MouseEvent>() {
    @Override
    public void handle(MouseEvent mouseEvent) {

        if (mouseEvent.getButton().equals(MouseButton.PRIMARY)) {
            if (mouseEvent.getClickCount() == 2) {
                ItemStep item = listViewVariable.getSelectionModel()
                        .getSelectedItem();
                if (item != null) {
                    try {
                    FXMLLoader isLoader = new FXMLLoader(Main.class.getResource("/view/itemStep.fxml"));
                    AnchorPane pane = isLoader.load();
                    Scene scene = new Scene(pane);
                    Stage stage = new Stage();
                    stage.setScene(scene);
                    item.setUrl(item.urlField.getText());

                    stage.show();

                    stage.setOnCloseRequest(new EventHandler<WindowEvent>() {
                        public void handle(WindowEvent we) {
                            item.setUrl(item.urlField.getText());
                        }
                    });
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }

            }
        }
    }
});

I continue to get the following error using the above. I need to be able use the FXML file within this Stage.

Caused by: java.lang.InstantiationException: controller.ItemStep
    at java.lang.Class.newInstance(Unknown Source)
    at sun.reflect.misc.ReflectUtil.newInstance(Unknown Source)
    ... 44 more
Caused by: java.lang.NoSuchMethodException: controller.ItemStep.<init>()
    at java.lang.Class.getConstructor0(Unknown Source)
    ... 46 more

Your ItemStep class does not have a no argument constructor .

Does a new instance of ItemStep get recreated after each double[click]?

Yes, that is what you have wrote your code to do.

If you didn't wish to do that, you should invoke the load method on your FXMLLoader outside of your event handler, store the reference to the loaded pane in a final variable, and use that reference inside the event handler, rather than loading a new pane time every time the event handler is invoked.

when I invoke both the load FXMLLoader and final AnchorPane outside the event handler I get: AnchorPane@ed39805[styleClass=root]is already set as root of another scene

You can't add a node to more than one scene or to a single scene more than once, you either have to remove it from the original scene, or just re-use the original scene with a single instance of the node. I don't know what kind of behavior you desire, but probably, you want to just have a single window pop-up and make it modal , rather than creating a new window every time somebody clicks on something.

Basically, you do something like this (though this is just an outline that I have never compiled or executed as I don't completely know your complete requirements, nor what your ItemStep and urlField really are):

final FXMLLoader isLoader = new FXMLLoader(Main.class.getResource("/view/itemStep.fxml"));
final AnchorPane pane = isLoader.load();
final Scene scene = new Scene(pane);
final Stage stage = new Stage();
stage.initModality(Modality.APPLICATION_MODAL);
stage.setScene(scene);

listViewVariable.setOnMouseClicked(event -> {    
    if (mouseEvent.getButton().equals(MouseButton.PRIMARY) && (mouseEvent.getClickCount() == 2)) {
        ItemStep item = listViewVariable.getSelectionModel().getSelectedItem();
        if (item != null) {
            stage.showAndWait();
            item.setUrl(item.urlField.getText());
        }
    }
});

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