permaculture playing cards*
The moose likes Applets and the fly likes If a user clicks the close box (X) on an applet window Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Applets
Bookmark "If a user clicks the close box (X) on an applet window" Watch "If a user clicks the close box (X) on an applet window" New topic
Author

If a user clicks the close box (X) on an applet window

Clayton Cramer
Ranch Hand

Joined: Aug 26, 2010
Posts: 41
I have a Javascript function that is calling a method in an applet. This works fine. The problem is that I have put the applet in a pop-up window. If the user clicks the close box, instead of hitting OK or Cancel, the method that is waiting for the user to make a button selection never returns. (It sits in a sleep loop waiting for the buttonPressed variable to go true.) It looks like the right solution is implement WindowListener, and when either the windowClosed or windowClosing methods are called, set buttonPressed to true. But the difficulty is that the windowClosed and windowClosign methods never seem to get called. Is there something that I am missing on this? Does Applet not used the WindowListener interface?
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18651
    
    8

Yeah... I saw your earlier code and I didn't like the idea of that infinite loop. Now I have a concrete reason for not liking it. So now I'm going to suggest you get rid of it and let your applet act like an applet.
Clayton Cramer
Ranch Hand

Joined: Aug 26, 2010
Posts: 41
Paul Clapham wrote:Yeah... I saw your earlier code and I didn't like the idea of that infinite loop. Now I have a concrete reason for not liking it. So now I'm going to suggest you get rid of it and let your applet act like an applet.


I would prefer not to use that approach, also. So, what is the right approach? I call the method, but I can't return until the user has hit OK or Cancel (or maybe the X box to close it). How should I do this? I do not see an obvious way to tell the method to wait until one of those terminating events have happened, except looping until I get such an event.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18651
    
    8

Clayton Cramer wrote:I would prefer not to use that approach, also. So, what is the right approach?

Just because you don't prefer to use an approach doesn't mean it's not the right approach.
I call the method, but I can't return until the user has hit OK or Cancel (or maybe the X box to close it). How should I do this? I do not see an obvious way to tell the method to wait until one of those terminating events have happened, except looping until I get such an event.

You have it backwards. Don't call the method until the user has already hit OK.

To do this the applet would have to communicate with your Javascript. When the user hits OK, the applet does something to the Javascript to permit it to call that method. Or better still, it just writes the relevant data out into a Javascript variable.

An applet GUI is designed to work in a particular event-based way. You are fighting against that way and that's why we have this big crop of posts from you. They are all because you are using the applet in a way it wasn't intended to be used. So I'm suggesting you stop fighting the applet and start getting along with it.
Clayton Cramer
Ranch Hand

Joined: Aug 26, 2010
Posts: 41
Paul Clapham wrote:
Clayton Cramer wrote:I would prefer not to use that approach, also. So, what is the right approach?

Just because you don't prefer to use an approach doesn't mean it's not the right approach.
I call the method, but I can't return until the user has hit OK or Cancel (or maybe the X box to close it). How should I do this? I do not see an obvious way to tell the method to wait until one of those terminating events have happened, except looping until I get such an event.

You have it backwards. Don't call the method until the user has already hit OK.

To do this the applet would have to communicate with your Javascript. When the user hits OK, the applet does something to the Javascript to permit it to call that method. Or better still, it just writes the relevant data out into a Javascript variable.

An applet GUI is designed to work in a particular event-based way. You are fighting against that way and that's why we have this big crop of posts from you. They are all because you are using the applet in a way it wasn't intended to be used. So I'm suggesting you stop fighting the applet and start getting along with it.


The Javascript starts the applet, and waits for a response. The applet in this case is a modal dialog. Is there some way for the applet's method to wait until the user clicks OK, Cancel, or hits the close box? I mean, other than looping on a sleep?
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18651
    
    8

Clayton Cramer wrote:The Javascript starts the applet, and waits for a response. The applet in this case is a modal dialog. Is there some way for the applet's method to wait until the user clicks OK, Cancel, or hits the close box? I mean, other than looping on a sleep?


You don't seem to understand the extent of my proposal. I am suggesting that the Javascript should just get whatever is in the text box at the time it is called. The user should already have keyed in the data before that Javascript is called. You don't need an OK button in the applet, and the applet doesn't need to be a modal dialog. The applet should simply be part of the screen, where the user can type their information, and the "OK" button should be an HTML button (or whatever HTML control you like) which causes the Javascript to get what's in the applet.
Clayton Cramer
Ranch Hand

Joined: Aug 26, 2010
Posts: 41
Paul Clapham wrote:
Clayton Cramer wrote:The Javascript starts the applet, and waits for a response. The applet in this case is a modal dialog. Is there some way for the applet's method to wait until the user clicks OK, Cancel, or hits the close box? I mean, other than looping on a sleep?


You don't seem to understand the extent of my proposal. I am suggesting that the Javascript should just get whatever is in the text box at the time it is called. The user should already have keyed in the data before that Javascript is called. You don't need an OK button in the applet, and the applet doesn't need to be a modal dialog. The applet should simply be part of the screen, where the user can type their information, and the "OK" button should be an HTML button (or whatever HTML control you like) which causes the Javascript to get what's in the applet.


The difficulty is that I am having to work within an existing framework: lots of JSPs that need a way to make a request to the user for some data. The text box only comes up when the Javascript requests the data. None of this steaming pile that I am cleaning up works the way that it should. It is very nearly a worst practices example.

I am trying to produce something that gives the functionality of the Javascript prompt function, but with a bit more control over the size of the input field. Maybe an applet isn't the right solution for this--but what I have works pretty well for everything except this one case: where the user clicks the X box instead of hitting Cancel.

I can see that stop and destroy are being called; I can set the variables to indicate that Cancel was pressed; the problem is that I don't have an obvious way to make sure that the sleep loop gets a chance to run to completion before returning from destroy. I told destroy to sleep for 500 ms in the hopes that the thread waiting on the button press would get control, but instead, whoever calls destroy interrupts the sleep.
Clayton Cramer
Ranch Hand

Joined: Aug 26, 2010
Posts: 41
Found the solution, although perhaps not the most elegant solution:

setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18651
    
    8

Good luck with your project then.
Clayton Cramer
Ranch Hand

Joined: Aug 26, 2010
Posts: 41
Paul Clapham wrote:Good luck with your project then.


I appreciate the help. I feel like I have been tasked with cleaning the Golden Gate Bridge with a toothbrush.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: If a user clicks the close box (X) on an applet window