JavaFX – set two ‘setonaction’ buttons to be the same

I get the EventHandler from another class. I want to set this EventHandler to the same button as other actionevents Is it possible?

The code is as follows The button is "btnadd"

public void addActionListener(EventHandler<ActionEvent> eventHandlerSetNotSave){
    btnAdd.setOnAction((e)-> {
        FileChooser fileChooser = new FileChooser();
        fileChooser.setTitle("Open Resource File");
        FileChooser.ExtensionFilter extFilter = new FileChooser.ExtensionFilter("pictures file format","*.TIF","*.JPG","*.PNG","*.GIF","*.JEPG");
        fileChooser.getExtensionFilters().addAll(extFilter);
        File file = fileChooser.showOpenDialog(null);

        if (file != null &&
                url.equals(emptyImageUrl)? true:alertMessageWarning("The viseme will change,and prevIoUs viseme will be delete.")) {
            setImage("file:"+file.toString());
            changeAfterSaved=false;
        }
    });
    btnAdd.setOnAction(eventHandlerSetNotSave);

    btnDelete.setOnAction((e)-> {
        changeAfterSaved=true;
        setImage(emptyImageUrl);
    });
}

Solution

Call the addeventhandler() method instead of setonaction (...):

btnAdd.addEventHandler(ActionEvent.ACTION,(e)-> {
    FileChooser fileChooser = new FileChooser();
    fileChooser.setTitle("Open Resource File");
    FileChooser.ExtensionFilter extFilter = new FileChooser.ExtensionFilter("pictures file format","*.JEPG");
    fileChooser.getExtensionFilters().addAll(extFilter);
    File file = fileChooser.showOpenDialog(null);

    if (file != null &&
            url.equals(emptyImageUrl)? true:alertMessageWarning("The viseme will change,and prevIoUs viseme will be delete.")) {
        setImage("file:"+file.toString());
        changeAfterSaved=false;
    }
});
btnAdd.addEventHandler(ActionEvent.ACTION,eventHandlerSetNotSave);
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>