Display confirmation when trying to close a Jinternalframe

Asked

Viewed 421 times

1

I’m creating a program using JInternalFrame, but would like to request a confirmation in case the user click the close button "Closable".

For that, I’m trying to overwrite the methods internalFrameClosed and internalFrameClosing, but still not quite sure how to proceed, I tried to put the setClosable(false) but it didn’t help.

  • Hello Juliano, check if the solution defined for this question helps you: https://stackoverflow.com/questions/18633164/how-to-ask-are-you-sure-before-close-jinternalframe

1 answer

1


You need to add one InternalFrameListener to the internal frame and overwrite the method internalFrameClosing(), because it is he who is called when you try to close the JInternalFrame.

But since this type of internal window has, by default, the behavior of being hidden when closing it, it is not useful to just implement something in this method, it is necessary to tell the window not to do anything when it is closed, thus leaving all the control in the override method. For this, just set the closing pattern:

seuFrameInterno.setDefaultCloseOperation(JInternalFrame.DO_NOTHING_ON_CLOSE);

And then add the Istener:

seuFrameInterno.addInternalFrameListener(new InternalFrameAdapter() {

    @Override
    public void internalFrameClosing(InternalFrameEvent e) {

        int op = JOptionPane.showInternalConfirmDialog(getInstance(), "Quer mesmo fechar essa janela?", "Fechar Janela",
                JOptionPane.YES_NO_OPTION);

        if (op == JOptionPane.YES_OPTION) {
            dispose();
        }
    }
});

Notice that I’m wearing showInternalConfirmDialog() since these are internal frames, there is no point in displaying a JOptionPane level of the main screen, and yes, only within the JDesktopPane. And it is mandatory to pass the internal frame instance as the first parameter. You can do this by creating a method getInstance() that returns the very instance of the class, as I did:

private Component getInstance() {
    return this;
}

See in operation:

inserir a descrição da imagem aqui

  • That’s exactly what I was looking for, thank you very much

  • @Julianosilveira :)

  • 1

    Downvoter, why don’t you show a little dignity and point out where the error is in the answer or what can be improved? Show that you really understand the subject, and negatively with the criterio of the post, and not by implication with me ;)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.