wood burning stoves 2.0*
The moose likes Swing / AWT / SWT and the fly likes communication between parent window and modal dialog box Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "communication between parent window and modal dialog box" Watch "communication between parent window and modal dialog box" New topic
Author

communication between parent window and modal dialog box

Bob Denaar
Greenhorn

Joined: Mar 21, 2011
Posts: 8
Hi,

This is probably a stupid question, but after all, it's a beginner's forum
So, i' programming an application which allows to choose a text file (with a specific content) and transforms it into a XML File. I also want to display, after the file has been selected, a modal dialog box which shows the advancement of the process.
Here are parts of the relevant code of the main window:



So, when a file is chosen, i want to display an instance of my custom dialog box, here are parts of the code of this box :



When I run this code, i can choose a file, and when i click on the "open" button of the JFileChooser, my custom modal dialog box is displayed, but the JTextField remains empty, despite the line : dialog.setProgress("Beginning XML transformation process") that comes after dialog.setVisible(true) in the code of the main window. The code that follows is not executed either
If however, i modify my dialog box code slightly by removing the line setModal(true), it works perfectly.

So, i suppose that when a modal dialog box is created, the execution of code in the parent window is blocked until the modal dialog is closed and that it's not the case when the dialog box is not modal (is it so ?)
But i'd like to keep my dialog box modal, so my question is simple : Is there any way to do what i want and keep the dialog box modal (i've heard about threads, but i don't know if it's the right thing to use, and frankly, it seems a bit overwhelming)

TIA
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38062
    
  22
Welcome to the Ranch

That is not at all a stupid question, but it sounds a specifically GUI-related question, so I shall move it.

Have you come across progress bars? Is that what you want? If you simply want a modal dialogue to appear saying started, try the message dialogue methods of an option pane. If you p ush ctrl-F "tutorial" you find both those links have Java™ Tutorial links in, which should be helpful to you.

Whenever you display a Swing component, make sure you have got it set up completely before you start. Or: put the setVisible() call after all other method calls. That might solve your problem.
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10172
    
    8

So, i suppose that when a modal dialog box is created, the execution of code in the parent window is blocked until the modal dialog is closed and that it's not the case when the dialog box is not modal (is it so ?)
But i'd like to keep my dialog box modal, so my question is simple : Is there any way to do what i want and keep the dialog box modal (i've heard about threads, but i don't know if it's the right thing to use, and frankly, it seems a bit overwhelming)


Looks like you figured out the secret!
Check out http://www.coderanch.com/how-to/java/JProgressBarDoesntUpdate
It should throw more light on how to work around the problem.

PS. From the usability point of view, I am with Campbell here. A JProgressBar would be more apt. From the user's point of view, he can close the dialog box. If the file conversion takes time, human curiosity being what it is (and impatience too), the user would wish to see the progress status. That would mean extra coding to handle this situation.


[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
Bob Denaar
Greenhorn

Joined: Mar 21, 2011
Posts: 8
Thank you both very much for your answers

After reading them (and the links you provided) and fiddling my code, i was able to devise a solution (by the way, you were right, a JProgressBar is what i needed), but there's still something i'm confused about.
Here is my modified code :




It works, but there's something i don't quite completely understand, namely the influence of modality. What happens exactly with the EventDispatchThread when i display (via setVisible) a modal dialog box ? My code works as it is, but if i remove the line from the main window and replace it with in the run() method of the dialog box, as soon as the dialog box is made visible, the rest of the code in the run() method is not executed, why ?

It's not crucial, but i like to understand what i do.


TIA
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38062
    
  22
Why are you accessing a JDialog outwith the event dispatch thread (=EDT)? If you look at the progress bar part of the Java™ tutorials, it says to do the slow task in a separate thread and leave all the Swing operations to the EDT.
Bob Denaar
Greenhorn

Joined: Mar 21, 2011
Posts: 8
I'm sorry, i don't understand your question very well, it seems to me (maybe i'm wrong) that i access the Dialog Box within the EDT (dialog.setVisible is called from an event handler method). What i was asking was a theoretical question concerning modality : what happens exactly with the EDT when i make a modal dialog box visible, and if i try for curiosity and understanding's sake to call setVisible from the second thread, the code that follows the call is not executed, I don't exactly understand why.

Thanks for spending time for me, apologies if these questions are too dumb.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: communication between parent window and modal dialog box
 
Similar Threads
Modal dialog box and request focus
returning String from ActionPerformed
Threading basics problem
Dealing with Jlist
Main JFrame doesn't always regain focus when modal dialog closes