Win a copy of Java Concurrency Live Lessons this week in the Threads forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Modeless JDialog  RSS feed

 
Dennis Putnam
Ranch Hand
Posts: 377
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm working on a modeless JDialog and am getting some conflicting information. As I read what modeless does it seems that it should display the dialog and permit the parent(s) to continue running. However, I have also found articles about the JDialog content not displaying properly (e.g. Modeless Not Displaying Content). The responses to those articles seem to imply that modeless does not really work due to "logical inflictions" (whatever they are) and a Thread or SwingWorker is necessary. If that is true then what does modeless really do/mean and what should I be using? TIA.
 
Rob Camick
Ranch Hand
Posts: 2756
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A modeless dialog works fine.

Try it and post your SSCCE when you have a problem. 

 
Dennis Putnam
Ranch Hand
Posts: 377
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks. It didn't take long to fall into the same trap as others. Indeed, my JDialog does not display correctly in modeless. It works fine if I set modal but then, of course, it blocks which I don't want.
 
Rob Camick
Ranch Hand
Posts: 2756
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your problem has nothing to do with a modeless JDialog (you would have problem with a JFrame as well with your code using your current structure).

Your problem is that you are using Thread.sleep(...) in code that is executing on the Event Dispatch Thread (EDT) which means the GUI can't respond to events and repaint itself.

There is no need for a Thread.sleep(...) when displaying a JOptionPane. A JOptionPane waits until the user closes the dialog.

There is also no need to use SwingUtilities.InvokeLater(...) twice.

Read the section from the Swing tutorial on Concurrency in Swing for more information.
 
Dennis Putnam
Ranch Hand
Posts: 377
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This was an SSCCE created from my main application to simplify it. That is why the invoke later shows up twice. In my application, that is essentially what happens as there is lots of other code running at the same time. The sleep was again for the purposes of creating the condition. In the application the dispose method is called from the parent when a particular condition is met. The intent of this JDialog is to let the user know it is waiting for that condition. This pane should not be closable by the user.
 
Campbell Ritchie
Sheriff
Posts: 53779
128
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Camick is right; Thread#sleep doesn't fit well with Swing GUIs on the Event Dispatch Thread.
 
Dennis Putnam
Ranch Hand
Posts: 377
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm open to suggestions on how else to reproduce this in an SSCCE?
 
Rob Camick
Ranch Hand
Posts: 2756
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm open to suggestions on how else to reproduce this


Reproduce what?

If you are trying to simulate a long running task on the EDT, the solution is simple, don't execute a long running task on the EDT.

You were given a link to the Swing tutorial for a reason.  Long running tasks need to execute on a separate Thread. The tutorial shows how to do this.
 
Dennis Putnam
Ranch Hand
Posts: 377
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm confused??? That was my original question. Do I need to use a thread or SwingWorker rather than modeless. The reply was that modeless works.

Define "long running." The condition it is waiting for normally takes a minute or less. Is that long? My SSCCE is only 10 seconds. Is that long?
 
Rob Camick
Ranch Hand
Posts: 2756
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your question is making no sense. and I gave you the best answer I could.

And I repeat, modeless has nothing to do with it.

You use a modeless dialog if you want the user to be able to interact with the frame and dialog at the same time.

You use a modal dialog if you don't want the user to interact with the frame while the dialog is display.

So you use the appropriate type of dialog for your requirement.

In either case long running code needs to execute in a separate Thread so you don't block the EDT.

Again, if you would post a proper SSCCE that demonstrates the problem you are having (as you have been asked to do several times in the past), then we can better understand your problem and not try to guess what you are thinking.

Based on the SSCCE provide the answer is straight forward don't execute long running code on the EDT.
 
Dennis Putnam
Ranch Hand
Posts: 377
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Obviously I am not asking the question correctly, if it makes no sense. I did post an SSCCE that reproduces the problem. If modeless has nothing to do with it then why does modeless mess up the display but work otherwise? I understand what you are saying about "long running" but I have no idea what constitutes "long." Less than a minute seems short to me. What I need is to pop up a message and leave it there until a certain internal condition is met (which means the dialog cannot block). I don't want the user to interact at all. Indeed I want to prevent the user from interacting until that dialog goes away. What I posted is from examples I found to accomplish that. If they are wrong then please point me to where I should look. I don't know what else to do beyond what has been suggested on various forums that seem to meet my objective. I am getting the impression that I need to use a thread or SwingWorker to do what I want. Can't someone simply say "yes" that is what you have to do or "no," modeless will work for short (<1 miniute) running processes?
 
Rob Camick
Ranch Hand
Posts: 2756
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have no idea what constitutes "long." Less than a minute seems short to me


Seriously? Have you never used an application before? I'm sure you get frustrated if it doesn't respond right away. Nobody is going to sit there for a minute waiting for something to happen.

Anything more than a second or so should be executed on a separate Thread. For example a database query or loading a file over the internet.

If you do have a long running task then you would probably use a JProgressBar so the user sees animation on the screen so they don't wonder what is taking so long. That way the user at least feels like something is happening in the background. For example when you down load a file you get regular updates on how much data has been download.

If you don't know how long something will take then you use an indeterminate progress bar.

The Swing tutorial has working examples of using a JProgressBar.

But even when you use a JProgressBar, the long running code still executes on a separate Thread.

Can't someone simply say "yes" that is what you have to do or "no," modeless will work for short


The type of dialog you use has absolutely nothing to do with the length of task that is being executed in a separate Thread.

You choose the dialog type based on how you want the user to interact with your application as I have already described.


 
Dennis Putnam
Ranch Hand
Posts: 377
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I give up. Thanks for what help you did provide. I've been programming for a long time and waiting for many seconds for web responses is common. You never asked what the context was for waiting and I didn't volunteer it because it did not occur to me that it mattered since you never said what was "long." In any case I was correct that the first response to my question was simply wrong. I am using a thread now and have pretty much resolved the problem.
 
Rob Camick
Ranch Hand
Posts: 2756
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In any case I was correct that the first response to my question was simply wrong.


The first response was indeed correct - a modeless dialog works fine when you code the logic properly and you use it the way it was designed to be used.

The fact that you don't know how to ask a question properly does not take away from the answer.

That is why YOU must always post a proper SSCCE with your question, so we can see what you are actually doing, not what you think you are doing.

I am using a thread now and have pretty much resolved the problem


Exactly as I suggested, once I saw the SSCCE.

 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!