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:
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
– escapistabr