• 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

JoptionPane Woes

 
Ranch Hand
Posts: 377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I seem to have some kind of mental block when it comes to understanding EDT and how to get JOptionPanes to reliably display. I have multiple instances of a class that do various checking when a particular method is called from a running thread. My "main" instantiates a JFrame which is the primary GUI. As part of the GUI initialization a thread is started that queries a web device to obtain data. That thread makes calls to that method in the appropriate instance of the classes. If a certain condition is detected in that method I want to display a JOptionPane. Thus the JOptionPane is not always dispatched so it can display because presumably the EDT never dispatches it. "Never" being the confusing part. Sometimes it displays as expected and other times it never displays. It seems like it should get dispatched eventually but that does not happen. I wonder what happens to that JOptionPane? It just sits in queue forever? In any case I don't know how to get this thing to display reliably. I read about SwingWorkers but either I don't understand them yet or that is not the solution either. Can someone explain why the JOptionPane "never" displays and if a SwingWorker is really the solution or something else. TIA.
 
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Don't really understand your problem, but from the API docs

All dialogs are modal. Each showXxxDialog method blocks the caller until the user's interaction is complete.


Try using System.out instead
 
Rancher
Posts: 3324
32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have never had a JOptionPane not display when I asked it to display.

So the problem is your code. Since you didn't post your SSCCE we can't help.
 
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rob Camick wrote:I have never had a JOptionPane not display when I asked it to display. . . .

Nor have I. But I have had option pane dialogues not appear. I have always found them eventually; they seem to have a strange habit of going to the back, so I sometimes had to minimise everything else on screen to find the dialogue window.
 
Dennis Putnam
Ranch Hand
Posts: 377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I take it from your answer that this is not a dispatch issue so a SwingWorker will not solve my problem. Thanks for that as that will avoid me chasing a red herring. Since this is an intermittent problem (I can run it several times before it fails) an SSCCE would be worthless plus the nature of the code makes an SSCCE that reproduces the problem impossible. I placed a System.out just before the 'ShowMessageDialog' and that gets executed but the dialog just never shows up. I'm not expecting anyone to look at my code and say here is what is wrong but rather I'm hoping for someone to suggest a debug technique. Maybe forcing a traceback at some point might help but where and when?
 
Dennis Putnam
Ranch Hand
Posts: 377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just to close this out, I did find the problem. It wasn't too hard once I stopped thinking it was a dispatch problem. I still have a problem with my understanding of when dialogs are displayed or not displayed due to the dispatcher but at least I have what I need now. Thanks again.
 
Campbell Ritchie
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It was good of you to explain the conclusion of the problem Maybe you would like to click the resolved button.
 
Rob Camick
Rancher
Posts: 3324
32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I still have a problem with my understanding of when dialogs are displayed or not displayed due to the dispatcher



Dialogs are displayed when you use setVisible(true), plain and simple. If you don't experience this behaviour then post your SSCCE.

The problem with all you questions is you keep making assumptions and you never post your code showing what you are actually doing.

Again, a problem is NOT a problem unless you can post a SSCCE demonstrating the problem.

The problem is with your code and we can't possibly guess what you may or may not be doing wrong.

You keep saying you can't reproduce the problem because your code is part of a bigger application. Well that is the problem with your questions. You don't really understand what you are asking because you don't know how to narrow down the problem.
 
Bartender
Posts: 732
10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Dialogs are displayed when you use setVisible(true), plain and simple.


Well, almost. The dialog may or may not get displayed if setVisibloe(true) is called from a thread that is not the Event Dispatch Thread.
 
Dennis Putnam
Ranch Hand
Posts: 377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I've said this many times. I don't post my code because it usually takes hundreds of lines and many classes to reproduce the problem. I am not looking for someone to examine my code most of the time. Rather I'm looking for suggestions on how to debug these problems or as in this case whether or not I'm chasing my tail. I agree with Fred because that is usually where I get trapped and why I was looking there to start. My problem with the understanding is that I cannot figure out how, when I am deep in the bowels of my code, to get dialogs onto the EDT.
 
Rob Camick
Rancher
Posts: 3324
32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You originally stated:

the JOptionPane is not always dispatched so it can display because presumably the EDT never dispatches it



So this tells us that the code is executed on the EDT and the answer is simple, you don't do anything special, just invoke the code to display the option pane.

I cannot figure out how, when I am deep in the bowels of my code, to get dialogs onto the EDT.



Now you are saying you don't know how to add code to the EDT, which is a completely different question. So which is it?

Again, the answer is simple:

1) If the code is executing in a Listener, then it is executing on the EDT.

2) If the code is executing on a Thread that you created then it is not executing on the EDT.

So, all you need to know is the design of your application to in order to know if the code is executing on the EDT or not. If you don't know this how do you expect us to know this?


if a SwingWorker is really the solution or something else.



A SwingWorker just does both. Code starts executing on a separate Thread and then from within the SwingWorker you control which code executes on the EDT. So you design the SwingWorker to meet your requirements. You are always in control.


 
Dennis Putnam
Ranch Hand
Posts: 377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks. That is exactly the kind of answer I am looking for which is not obvious when reading the documentation.
 
No thanks. We have all the government we need. This tiny ad would like you to leave now:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic