jQuery in Action, 2nd edition*
The moose likes Applets and the fly likes Displaying a JOptionPane in the stop method? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Applets
Bookmark "Displaying a JOptionPane in the stop method?" Watch "Displaying a JOptionPane in the stop method?" New topic
Author

Displaying a JOptionPane in the stop method?

Heath Lilley
Ranch Hand

Joined: Jan 09, 2001
Posts: 72
Hello all,
I am having a minor issue with displaying a "save confirmation" popup, when it is executed from the stop method of an applet.
Here is the deal, I have an applet that draws a rectangular drawing based on user input (i.e. mouse clicks and arrow keystrokes). There is a save button that sends the data to a servlet that updates a database, but some times the users forget to save and leave the applet's page, so their changes are not saved. There are instances where they may not want to save the changes so I can't automatically save the drawing.
Now here is my solution... I detect whether they have made any changes by setting a boolean value in certain event handlers. I then use a popup to display a JOptionPane.shoConfirmationDialog() when the applet's stop method is called (i.e. There are links to other areas of the application on the applets page which would call a different page).
My problem is that sometimes the popup is not getting focus and is not displayed "ON TOP" of the browser window. I am pretty sure that its because the applet is running in its own separate thread than the browser window on the clients machine. Another issue, which is probably related to the above issue, is that even when the popup is displayed the browser still loads URL that was called before the user has clicked an option on the popup.
Now my gut is telling me there is only one thing I can do to rectify the situation... make all of the links on the page part of the applet itself. That way the call to a URL can be suspended while the popup is displayed. The only problem is that the application is running in a framed window and there are menu links that would be difficult to put on the applet.
Is this a valid approach or is there a better way?
Here is the stop() method’s code...

Thanks in advance.
Heath
[ August 05, 2002: Message edited by: Heath Lilley ]
[edited by Rob Ross to reduce width of post]
[ August 05, 2002: Message edited by: Rob Ross ]
Rob Ross
Bartender

Joined: Jan 07, 2002
Posts: 2205
A couple of things:

first, I'm not exactly sure what you mean by "popup"; are you talking about an actual pop-up menu, or do you just mean you're "popping up" a dialog?
Second, if you want to display a modal dialog that is on top of all other windows, instead of passing this, pass a reference to the window that the applet is drawn into. Try passing getParent() instead.
Finally, you should not be drawing any UI code during the stop() method, for the reason you gave. You are not being given the "option" to stop(), you are just being notified that your applet is being stopped, for whatever reason. You really don't want to try dialoging with the user at this point. One solution I would suggest is having some kind of preference set so that it automatically saves your changes when you leave the page; ie, deal with your user/computer dialog before the stop() call occurs, then just do whatever the user indicated was their preference.
good luck!


Rob
SCJP 1.4
Heath Lilley
Ranch Hand

Joined: Jan 09, 2001
Posts: 72
Rob,
Thanks for the reply. The getParent() is a good idea.
Well the thing is that there is a chance that they would go in and make changes and NOT want to save.
I will have to figure something else out. But thanks.
Heath Lilley
Ranch Hand

Joined: Jan 09, 2001
Posts: 72
Rob,
Incase you were intrested I found a fix. I am going to use a Javascript window.confirm() to dislplay the dialoge message. That way there is no UI code in the stop method.
In my javascript function...
I just use document.applets.isDrawingHasChanged() to test for changes. And document.applets.saveDrawing() to save the drawing if they click OK.
THis works with IE 5.5 (this is a intranet app and we standardized on IE 5.5). For netscape and others u could use some sort of modal/popup window to do the same thing.
Thanks for the help though.
 
 
subject: Displaying a JOptionPane in the stop method?
 
Similar Threads
jsp using applet
A backend thread, need advice
Loading of applets in browser
Saving HTML file
jsp using applet