g0rdonL
g0rdonL

Reputation: 303

JavaFX fire close tab event

tab.setOnCloseRequest(e -> {
                e.consume();
                if (currentEditor.isModified()){
                    switch (DialogBox.newSaveConfirmationBox(tab.getText())){
                        case "Yes": tabPane.fireEvent(???);
                    }
                }
            }

I would like to make a confirmation for a tabbed text editor, where there are opetions "yes" "no" cancel". Is there a way to fire a close tab event when "yes" is selected? thanks

tab.setOnCloseRequest(e -> {
                if (currentEditor.isModified()){
                    switch (DialogBox.newSaveConfirmationBox(tab.getText())){
                        case "Yes":
                            saveFile();
                        case "Cancel":
                            e.consume();
                    }
                }
            }
    );

Sadly, when currentEditor.isModified() is false, the tab can't be closed, any suggestions? thanks

Upvotes: 2

Views: 1438

Answers (1)

James_D
James_D

Reputation: 209714

Consuming the event vetoes the close. So you should only consume the event if the user decides they do not want to close the tab. Otherwise, let the event propagate, and the tab will close:

tab.setOnCloseRequest(e -> {
    // remove the next line: you only want to consume the event for "No"
    // e.consume();
    if (currentEditor.isModified()){
        if ("No".equals(DialogBox.newSaveConfirmationBox(tab.getText()))) {
            e.consume();
        }
    }
});

Upvotes: 3

Related Questions