Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaFx - Set two 'setOnAction' to the same button

I get EventHandler from another class, and I want to set this EventHandler and other ActionEvent to the same button. It is possible?

The code is following. 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);
    });
}
like image 483
Ramz Avatar asked Jan 08 '23 12:01

Ramz


1 Answers

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",
            "*.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.addEventHandler(ActionEvent.ACTION, eventHandlerSetNotSave);
like image 160
James_D Avatar answered Jan 22 '23 07:01

James_D