2

Possible Duplicate:
How do I run JOptionPane on EDT?

At seemingly random times (i.e. if the program is run enough times) a JOptionPane window I have programmed will display a blank window the close & ok buttons are there but none of the text I coded. This seems to occur in any program written with JOptionPane eventually; is there some way to prevent this? FWIW This rarely (if ever) occurs twice in a row.

I wrote this just now and ran it 15 times in a row without any problems, yet. It's exactly the same coding as I always use when using JOptionPane. Is there something missing?

import javax.swing.JOptionPane;
public class SimpleJOptionPane
{
    public static void main(String[] args)
    {
        JOptionPane.showMessageDialog(null, "Hello, World!");
    }//end main
}//end class SimpleJOptionPane

Generally this problem does not occur. The JOptionPane usually displays with the intended text. What could have changed without my knowledge when it doesn't work?

As suggested in a comment, I am looking into using EventQueue.isDispatchThread()

The code below, as you can see, uses another method to display the JOptionPane. Is this an adequate solution?

final String ERR_TITLE = "Error";
final String ERR_MSG = "An exception has occured; please start over.";
                    showError(ERR_MSG, ERR_TITLE);

public static void showError(final String MESSAGE, final String TITLE)
{
    JOptionPane.showMessageDialog(null, MESSAGE, TITLE, JOptionPane.ERROR_MESSAGE);
}//end showError
Community
  • 1
  • 1
Matt B
  • 1,813
  • 2
  • 17
  • 20
  • @james It isn't happening in any particular program with any degree of consistency. Rather it occurs as aforementioned seemingly randomly in any program I have ever written using JOptionPane. I could post any one of them if you like, how much code would you need to see? – Matt B Jul 15 '12 at 07:15
  • 4
    have you checked this? http://forum.springsource.org/showthread.php?78603-JOptionPane-blank-(white) – Mushahid Hussain Jul 15 '12 at 07:29
  • 1
    Might you not performing Swing Related tasks on Event Dispatch Thread, Please have a look at [Concurrency in Swing](http://docs.oracle.com/javase/tutorial/uiswing/concurrency/index.html). Without the [SSCCE](http://sscce.org/), that is my best guess :-) – nIcE cOw Jul 15 '12 at 07:47
  • @MattB : You not calling your code on the `EDT - Event Dispatch Thread` never use Swing related stuff inside the `main()` Method, or any other `Thread`. – nIcE cOw Jul 15 '12 at 08:17
  • What video card are you using? Are te drivers up to date? Tere is a known issue with ati cards tht can cause blank screens (typically black) – MadProgrammer Jul 15 '12 at 09:23
  • @Mad The screen is not blank, just the `JOptionPane` – Matt B Jul 15 '12 at 10:49
  • Sorry, I mean the "window" contents is empty – MadProgrammer Jul 15 '12 at 20:13
  • @mad I don't think it's the video card. Nothing is black; the JOptionPane renders properly, however, there is no text in it. – Matt B Jul 15 '12 at 22:19
  • 1
    @MattB No probs, always worth a check ;) – MadProgrammer Jul 15 '12 at 22:49
  • I don't think this is necessarily a duplicate of [link]http://stackoverflow.com/questions/13093499/how-do-i-run-joptionpane-on-edt[/link] I'm experiencing this exact bug and am 100% sure that everything is being handled in the EDT. I can't vote to reopen due to lack of rep however. – JeroenWarmerdam Jan 21 '13 at 13:40

1 Answers1

3

If I were you, this is what i'd do:

  1. Create a static method that delegates do JOptionPane
  2. Make sure that all my code refers to that method and NOT to JOptionPane
  3. In that static method check for empty strings and nulls.

-

public static void myShowMessage(String s) {
    if(s == null || "".equals(s)) {
        System.out.println("AHA!  gotcha ...");
    }
    JOptionPane.showMessageDialog(null, s);
}

I know this may sound a bit silly, but from experience this is not a problem in the API itself ...

nIcE cOw
  • 24,468
  • 7
  • 50
  • 143
daniel_or_else
  • 658
  • 6
  • 11
  • 1
    A slight mistake, either way the control will reach `showMessageDialog(...)`, so why the check, we never put `showMessageDialog(...)` inside the else block :-) – nIcE cOw Jul 15 '12 at 08:25
  • 1
    The idea is just to check when your code attempts to send a null or empty string. No more than that ... – daniel_or_else Jul 15 '12 at 08:28
  • @daniel While I will stipulate that what you suggested is very prudent, in theory, what happens if your method in particular detects a null or empty string? The JOptionPane is displayed either way. Also, if I have created a message dialog and coded a string into it, how is there any risk of that string being null or empty? – Matt B Jul 15 '12 at 22:46
  • 1
    The point is to rule it out. I'd also put a EventQueue.isDispatchThread() check just as another possible location of an error – MadProgrammer Jul 15 '12 at 22:51
  • @Mad What good is ruling it out if the JOptionPane is displayed anyway? – Matt B Jul 15 '12 at 23:00
  • 1
    GUI updates should be done on the EDT (Event Dispatch Thread) - this problem of 'intermittent failure' is a classic sign of not obeying that rule. See [Concurrency in Swing](http://docs.oracle.com/javase/tutorial/uiswing/concurrency/) for more details. – Andrew Thompson Jul 15 '12 at 23:18
  • 1
    @MattB so you can determine where the problem actually occurs. IE - is because you've accidentally sent a blank value to the screen, or is it because it's an ETD issue or is it a bug?? – MadProgrammer Jul 16 '12 at 01:54
  • @daniel how does _your code_ make use of the `EDT`? Is using another method to show the `JOptionPane` all it takes to solve this? – Matt B Jul 16 '12 at 02:04