• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to Set up a JOptionPane that is close only programmatically  RSS feed

 
Dennis Putnam
Ranch Hand
Posts: 377
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've previously asked questions about problems getting a JOptionPane to display the way I need but I simply cannot get anything to work because of Swing threading and some kind of mental block trying to understand it. I guess I need to go back to the beginning and seek help on how to approach a solution. I have a GUI (JFrame) that needs to do some initialization that includes waiting for responses from a web server. This GUI is started thus:

While the GUI is initializing, I want to display a message telling the user to wait and prevent the user from closing that message. The closest I have come is using a 'Thread' where the JOptionPane opens and displays but is blank (content not showing). I just don't understand how to get the GUI to let this pane fully display before proceeding with the initialization. TIA.
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What you mean is a Splash screen
Recommended reading: https://docs.oracle.com/javase/tutorial/uiswing/misc/splashscreen.html

PS. Same effect can also be achieved using a JWindow.
 
Dennis Putnam
Ranch Hand
Posts: 377
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the reply. Just to clarify, are you saying that JWindow (or SplashScreen) is handled differently than JOptionPane (or JDialog) via thread with respect to when it can run? In other words a JWindow can run without being blocked, in my case, by GUI while JOptionPane is?
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No. These are two different type of objects. The advantage is you do not need to worry about removing any buttons which can enable the user to close it, unlike the JDialog
The link I posted has a working example SplashDemo.java Check it out to see how easily the SplashScreen can be used
 
Dennis Putnam
Ranch Hand
Posts: 377
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks but that is not really my issue. I can create the message pane as I want, I just can't get it to display properly while my GUI is doing its initialization.
 
Dennis Putnam
Ranch Hand
Posts: 377
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A thought just occurred to me. Would I be able to accomplish this through an 'actionlistener' in my GUI that does nothing but display the message then discard the listener later to get rid of it?
 
Brett Spell
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
JOptionPane is just a convenience "wrapper" class that creates cookie-cutter instances of JDialog. If I understand correctly what you're trying to do, the easiest way to accomplish it is just to create your own JDialog and use setDefaultCloseOperation() to specify that attempts to close it should be ignored.
 
Dennis Putnam
Ranch Hand
Posts: 377
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the reply but please read my earlier response (7/25/2016 12:43:31 PM). Unfortunately I did not realize I had a misleading subject soon enough to edit it. Again the problem is not preventing the user from closing it. The problem is the content of the JDialog does not display properly in the JOptionPane. The parent thread does not let it run in order to complete the display. I get a blank JOptionPane.
 
Brett Spell
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Without an SSCCE, I can only speculate as to what's wrong. My guess would be that either you aren't constructing the component(s) correctly or that you're doing something that blocks the Event Dispatch Thread (EDT), preventing it from painting / displaying the content.
 
Dennis Putnam
Ranch Hand
Posts: 377
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That is the crux of my question. I know the EDT is being blocked but I don't understand how to NOT do that. I have my main GUI (JFrame) running with 'invokelater' from main. That is what needs to continue running for my initialization and what tries to display the JOptionPane in a thread during that process. I understand that because the JFrame is running it is preventing the JOptionPane from being dispatched but what I need to know is how to not block the JOptionPane, at least until it is fully displayed and let the JFrame keep running too. Here is the code that sets up the JOptionPane:

Here is how I call it from my JFrame initialization method:
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!