JDialog problem with showing its content when modal==false.

I want to have JDialog popping up in my game showing some info and then disappear after 2-3 seconds. Obviously, having a modal JDialog with a “OK” button to close it will get hazzling so therefore I want no such button instead close it automatically.

So, I need show() followed by a sleep and then hide(). If I use modal, then i need the sleep inside the JDialog since modal JDialog stops the outside code until it hide(). But, if I instead use a non modal one and put the sleep outside the JDialog and then close it from outside as well, i get a strange way of the JDialog not painting itself. The text does not get shown on the JDialog unnless it is Modal. Strange. I get kind of the same problems I had (since i use passive rendering) of having to force paint with paintImmediately but in JDialog there is no such method I can use?

What to do?

read up on the eventque, if you are making the the thread that paint the Dialog sleep then offcourse it isn’t gonna paint.

also rolling your own sleep(inteval)…action is also not needed have a look at Timer resp TimerTask

The thing is, inside my class extending jdialog i had

public void show(String text)
{
this.jLabel1.setText(text);
this.show();
XThread.delay(1500); /same as, try sleep catch exception/
this.hide();
}

If the object is modal==false when running this method, it shows, waits 1.5 seconds and then closes as intended. If modal==true then it does not close, why?

I haven’t worked with this stuff before… how about simple compileable example of the problem? I can’t truly understand the problem.

Swing is SINGLE-threaded, i.e. if you put the Swing thread to sleep using “XThread.delay(1500);” - nothing will happen, i.e. no redraws anything. Your sleep call puts Swing to sleep.

Instead, create a separate thread which hides the dialog box after a predefined time:


Thread sleepThread = new Thread() {
  public void run() {
    Thread.sleep(1500);
    dialog.setVisible(false);
  }
};
// start hiding thread and continue with current (Swing) thread
sleepThread.start();

// 


The real answer is that dialog.show() or dialog.setVisible(true) is blocking when the dialog is modal - as long as the dialog is visible.

If you try to run the closing-code it after dialog.show() that code will never run.