• Post Reply Bookmark Topic Watch Topic
  • New Topic

Modal dialog issue  RSS feed

 
Oscar Paesi
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I have the following scenario, that is working:
- Some task is launched from a JFrame. It may take a long time to perform or may not;
- To perform the task I use a background thread, since I don't want the interface to be frozen while the task performs;
- I use a modal JDialog to show the user some feedback about the task state and set it to modal, so I prevent the following JFrame code from being executed before the task is done , since the modal dialog blocks the Event-dispatch thread;

I want the JDialog to wait for some time before it is shown, because if the task takes less than a specified time, say 500ms, there's no need to pop up the dialog.
But, if I don't call JDialog.setVisible(true) immediately after launching the background thread, the GUI won't be blocked and the following code will be executed.
Is there any way to block the Event-dispatch thread and use the JDialog mechanism to prevent the GUI from freezing without calling JDialog.setVisible(true)?

Thanks in advance!
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
> But, if I don't call JDialog.setVisible(true) immediately after launching the background thread,
> the GUI won't be blocked and the following code will be executed.

put "the following code" into a separate method, and call it from the separate thread (when it's finished),
but call it via the EDT - SwingUtilities.invokeLater(..)

 
Oscar Paesi
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for your reply, Michael.
Unfortunatelly that won't help me, because the code after the thread launching could be simply the end of the method. So it will return and continue to execute, what is undesired.
Any other idea?
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
so, the separate thread could return immediately, but you don't want the code that follows to execute immediately, preferring some delay.

if so, possibly a timer to call the separate method might work, but it's difficult to understand the need for a delay.

perhaps if you could post a small demo program (simulating the separate thread if its a db connection, or similar),
that shows why the delay is required, it might be easier to understand, and better solutions might be posted.
 
Oscar Paesi
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I'm sorry for taking so long to reply.

I made a simple demo to illustrate what I'm trying to do, as suggested.
To run the background task, I use a SwingWorker instance .
The main goal of this approach is giving better feedback to the user. He should never get the impression that the program is frozen.
Why don't I show the dialog immediately? Because I'm using a cache system on this project and the task usually takes a long time when I'm loading the data from the database for the first time. After that, if I run the task again, it will execute almost instantly and the dialog will just blink, what looks really weird to the user. So, I have to wait for some time before showing it.

 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
see if this is closer to what I think you're trying to do

basically just moved a few things around, and added an if condition to launch the swingworker (once data cached you don't want the swingworker any more?)
also disabled button until after executeRestOfCode()
 
Oscar Paesi
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, it isn't.

I just gave a few reasons why I need this delay. Actually the hole stuff is much more complicated than what is shown in the example.
I have a class to launch a generic task, which extends SwingWorker and shows the dialog. The dialog is not exposed to the class user. He only overwrites doInBackground() an other SwingWorker methods he needs. I don't have control over the task itself.
When the user calls execute(), the background thread is launch and the timer which will show the dialog is started. The execute() returns and continue to execute what comes next, until the timer shows the dialog.
A terrible solution I found is set the dialog size to a pixel (almost invisible) and make it visible. When the timer action is fired, I restore the dialog size with pack().
I have the modal effect I want and the user doesn't see the dialog blinking when the task executes too fast.

Thank you for your help anyway, Michael.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!