-1

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?

Goralight
  • 2,067
  • 6
  • 25
  • 40
mikel
  • 3
  • 2

1 Answers1

0

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) .

See : JInternalFrame.setDefaultCloseOperation

Arnaud
  • 17,229
  • 3
  • 31
  • 44
  • Thank you. I already have this structure and DO_NOTHING_ON_CLOSE is also enabled. Still, I would like to distinguish if the call to the method internalFrameClosing came from X button or from other method (e.g. by calling internalFrame.dispose();) – mikel Jan 20 '17 at 16:14
  • I just tested , calling `dispose` will call `internalFrameClosed` , but will NOT call `internalFrameClosing` , so you know that the `internalFrameClosing` call only comes from the X button, if your programmatical closing is always performed by a call to `dispose`.. – Arnaud Jan 20 '17 at 17:50