I have a JInternalFrame which could be closed by clicking on the X button or programmatically from the menu. Both approaches end up in
public void internalFrameClosing(InternalFrameEvent e)
and later
public void internalFrameClosed(InternalFrameEvent e)
I would like to distinguish the source of this call and trigger different actions (i.e. in the case of closing my frame by X button ask for confirmation and later dispose(), in the case of selecting "close" from the menu just dispose() the frame).
Could you suggest any solution?
To change the behavior of the X button, you may set the default close operation to DO_NOTHING_ON_CLOSE
, and add an InternalFrameListener
which will take care of asking for closing confirmation, and closing the frame if needed :
internalFrame.setDefaultCloseOperation(JInternalFrame.DO_NOTHING_ON_CLOSE);
internalFrame.addInternalFrameListener(new InternalFrameListener() {
@Override
public void internalFrameOpened(final InternalFrameEvent e) {
}
@Override
public void internalFrameClosing(final InternalFrameEvent e) {
// Do your confirmation stuff !!
// Dispose the internal frame if needed !!
}
@Override
public void internalFrameClosed(final InternalFrameEvent e) {
}
@Override
public void internalFrameIconified(final InternalFrameEvent e) {
}
@Override
public void internalFrameDeiconified(final InternalFrameEvent e) {
}
@Override
public void internalFrameActivated(final InternalFrameEvent e) {
// TODO Auto-generated method stub
}
@Override
public void internalFrameDeactivated(final InternalFrameEvent e) {
// TODO Auto-generated method stub
}
});
DO_NOTHING_ON_CLOSE
Do nothing. This requires the program to handle the operation in the windowClosing method of a registered InternalFrameListener object.
(note that in the above documentation extract, I suspect that windowClosing
is a typo, they probably meant internalFrameClosing
) .