g0rdonL
g0rdonL

Reputation: 303

JAVAFX tab onCloseRequest consumes tabPane

tab.setOnCloseRequest(e -> {
                if (getEditorForTextArea(getSelectedTextArea()) != null){
                    selectedTextArea = getSelectedTextArea();
                    selectedEditor = getEditorForTextArea(selectedTextArea);
                    if (selectedEditor.isModified()){
                        if (DialogBox.newSaveConfirmationBox(tab.getText()))
                            saveFile();
                        else e.consume();
                    }
                }
            }
    );

This is my code. I wanted to pop up a saveConfirmationBox when selectedEditor.isModified(). Then a problem occured. I opened two tabs, one modified and another unmodified. When i am selecting the tab which is unmodified, I can close the modified tab with the X button without any confirmation. On the other hand, the confirmation box showed up when I am on the modified tab trying to close the unmodified one.

Here is how i get the selectedEditor:

private Editor getEditorForTextArea(TextArea textArea) {
    Iterator<Editor> editorIterator = editorVector.iterator();
    while (editorIterator.hasNext()) {
        Editor editor = editorIterator.next();
        if (textArea == editor.getRoot())
            return editor;
    }
    return null;
}

@Nullable
private TextArea getSelectedTextArea() {
    SingleSelectionModel<Tab> selectionModel = tabPane.getSelectionModel();
    if (selectionModel.isEmpty())   return null;
    Tab selectedTab = selectionModel.getSelectedItem();
    return (TextArea)selectedTab.getContent();
}

Thanks for help in advance :)

Upvotes: 1

Views: 505

Answers (2)

g0rdonL
g0rdonL

Reputation: 303

Thanks @Vyacheslav Zhukov, here is my solution:

tab.setOnCloseRequest(e -> {
                if (getEditorForTextArea(getTextAreaForTab(tab)) != null){
                    Editor editorToClose = getEditorForTextArea(getTextAreaForTab(tab));
                    if (editorToClose.isModified()){
                        if (DialogBox.newSaveConfirmationBox(tab.getText()))
                            saveFile();
                        else e.consume();
                    }
                }
            }
    );

private TextArea getTextAreaForTab(Tab tab) {
    return (TextArea)tab.getContent();
//PS it won't be null lol nothing to check here :P
}

Upvotes: 0

Viacheslav Zhukov
Viacheslav Zhukov

Reputation: 1350

I think this is totally expected behaviour because you are getting TextArea of currently selected tab in getSelectedTextArea() method. And so it checks Editor of currently selecetd tab, not the one that you are trying to close.

You should modify your setOnCloseRequest to comething like:

tab.setOnCloseRequest(e -> {
                if (getEditorForTextArea(getTextAreaFor(tab)) != null){
                    textArea = getTextAreaFor(tab);
                    editor = getEditorForTextArea(textArea);
                    if (editor.isModified()){
                        if (DialogBox.newSaveConfirmationBox(tab.getText()))
                            saveFile();
                        else e.consume();
                    }
                }
            }
    );

And the method getTextAreaFor(tab) will be

private TextArea getTextAreaFor(Tab tab) {
  return (TextArea)tab.getContent(); // with checks etc...
}

Upvotes: 1

Related Questions