Prevent pop-up window from closing

Hi,

I’m working on a code where I want user to confirm, “Are you sure you want to close this pop-up. any unsaved data will be lost!” OK / Cancel.

I’m able to give a popup with “OK” button on visionWindowClosed event, but I do not find a way to stop a window from closing.

Try this:

from javax.swing import JOptionPane

pane = JOptionPane()

strMessage = "<html><center>Are you sure you want to close this pop-up?<br><br>Any unsaved data will be lost!"
strTitle = "Confirm Close"
frame = None

btnOpts = JOptionPane.OK_CANCEL_OPTION
msgType = JOptionPane.WARNING_MESSAGE
icon = None
options=["OK", "Cancel"]

idxResult = pane.showOptionDialog(frame, strMessage, strTitle, btnOpts, msgType, icon, options, options[1])

If idxResult == 0:
   [Close window code]

For more info on Java swing dialogs, see: http://docs.oracle.com/javase/tutorial/uiswing/components/dialog.html

Thanks for your reply. But this will work with a custom close button. what i’m trying to achieve is on the default “X” close button.

Ok, so you need to take 2 steps. First, you need to disable the default close action on the x button. To do that, place this inside internalFrameOpened on the popup window:

from javax.swing import WindowConstants
event.source.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE)

Then you need to present an option to close the window or not, and then call dispose() on the window, as we have overridden the close command:

from javax.swing import JOptionPane

parent = event.source
message = "Are you sure to close this window?"
title = "Really Closing?"
optionType = JOptionPane.YES_NO_OPTION
messageType = JOptionPane.QUESTION_MESSAGE
if JOptionPane.showConfirmDialog(parent,message, title,optionType,messageType) == JOptionPane.YES_OPTION:
	event.source.dispose() 

Happy Hacking!!

2 Likes