This week's book giveaway is in the Jython/Python forum.
We're giving away four copies of Murach's Python Programming and have Michael Urban and Joel Murach on-line!
See this thread for details.
Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Closing dialog box using windows listener  RSS feed

 
Ravikanth kolli
Ranch Hand
Posts: 179
Chrome Eclipse IDE Mac
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi
i have the following situation:
The user clicks a button and a dialog is displayed showing the status of the computations that it is undergoing in the background. It is an iterative step.

I would like to add a feature where i will have to close the dialog and stop the execution of the computations on clicking the 'X' button on the top right corner.

I read that windows listeners work for dialogs too so i tried using it.

I wrote the following code in the loop in which the program iterates.



but this one doesnot work. I am working in swings.

Do you have any better ideas?

Thanks
 
pete stein
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm confused. Isn't putting dialog.dispose() within the dialog's windowClosing method a bit redundant? It's already going to close without this method (unless I'm missing something basic here). I'm gathering that what you want is to stop whatever process is running on window close, is that correct? If so then calling close on the dialog will do nothing of this sort. How you do this will depend entirely on the rest of your program in the code not shown here.

You might want to spend a bit of time clarifying this question a bit, and even posting a small compilable program that illustrates your issues.
 
Ravikanth kolli
Ranch Hand
Posts: 179
Chrome Eclipse IDE Mac
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
well, yaa i too think that putting dialog.dispose() will make the code redundant.
I'm gathering that what you want is to stop whatever process is running on window close, is that correct?
yes, you are right.



I am not sure of the accuracy of the example but the question is
I want to close the dialog in between the 10000 iterations when the user clicks the 'X' on the dialog. Can you tell me a way to do that?
 
Rob Spoor
Sheriff
Posts: 20893
81
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by pete stein:
I'm confused. Isn't putting dialog.dispose() within the dialog's windowClosing method a bit redundant? It's already going to close without this method (unless I'm missing something basic here).

Actually the default behaviour is hide the dialog, not dispose it. To dispose by default you'll have to call setDefaultCloseOperation(DISPOSE_ON_CLOSE) first.

Ravikanth, you'll need a boolean flag inside the loop as well that will be set to false by the closing. Most likely you'll have to create a method for setting it to false. Something like
<blockquote>code:
<pre name="code" class="core">for (int i = 0; running && i < 10000; i++)
{
text.append(i);
dialog.validate();
}
</pre>
</blockquote>
The window adapter will then set running to false through some method you'll have to create.
 
Ravikanth kolli
Ranch Hand
Posts: 179
Chrome Eclipse IDE Mac
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tried using a boolean variable to check if the event is invoked.

I tried testing it using a print statement in the listener, I do get the print after the complete program is executed, which means that the window listener is not invoked until the program completed its execution.

Is there any way out of this?
 
Rob Spoor
Sheriff
Posts: 20893
81
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You have to use a different thread for the calculation itself. That also includes using EventQueue.invokeLater and EventQueue.invokeAndWait.

Please check out javax.swing.SwingWorker for a more convenient way of handling this. I think the publish / process methods can really help you out - you publish "i" each time, and override process to handle it.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!