Help coderanch get a
new server
by contributing to the fundraiser
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Closing dialog box using windows listener

 
Ranch Hand
Posts: 179
Mac Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Mac Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Sheriff
Posts: 22791
131
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Mac Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 22791
131
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
The longest recorded flight time of a chicken is 13 seconds. But that was done without this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/t/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic