# Prevent pop-up window from closing

**URL:** <https://forum.inductiveautomation.com/t/prevent-pop-up-window-from-closing/15630>\
**Category:** Ignition\
**Created:** [October 10, 2017, 8:24pm UTC](https://forum.inductiveautomation.com/t/prevent-pop-up-window-from-closing/15630 "2017-10-10T20:24:14Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![RohanPalshikar](https://avatars.discourse-cdn.com/v4/letter/r/7ba0ec/32.png) [@RohanPalshikar](https://forum.inductiveautomation.com/u/RohanPalshikar)\
**Post date:** [October 10, 2017, 8:24pm UTC](https://forum.inductiveautomation.com/t/prevent-pop-up-window-from-closing/15630/1 "2017-10-10T20:24:14Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![pphillips](https://avatars.discourse-cdn.com/v4/letter/p/da6949/32.png) [@pphillips](https://forum.inductiveautomation.com/u/pphillips)\
**Post date:** [October 10, 2017, 9:42pm UTC](https://forum.inductiveautomation.com/t/prevent-pop-up-window-from-closing/15630/2 "2017-10-10T21:42:05Z")

</div>

Try this:

```auto
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](http://docs.oracle.com/javase/tutorial/uiswing/components/dialog.html)

---

<div class="post-metadata">

**Author:** ![RohanPalshikar](https://avatars.discourse-cdn.com/v4/letter/r/7ba0ec/32.png) [@RohanPalshikar](https://forum.inductiveautomation.com/u/RohanPalshikar)\
**Post date:** [October 11, 2017, 9:25pm UTC](https://forum.inductiveautomation.com/t/prevent-pop-up-window-from-closing/15630/3 "2017-10-11T21:25:30Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Kyle\_Chase](https://avatars.discourse-cdn.com/v4/letter/k/9de0a6/32.png) [@Kyle\_Chase](https://forum.inductiveautomation.com/u/Kyle_Chase)\
**Post date:** [October 12, 2017, 7:10am UTC](https://forum.inductiveautomation.com/t/prevent-pop-up-window-from-closing/15630/4 "2017-10-12T07:10:32Z")

</div>

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:

```auto
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:

```auto
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!!
