Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pause execution while JDialog is open

Tags:

java

swing

How can I make my application pause when I open custom JDialog and after the dialog is closed to make continue again.

like image 429
Jakub Arnold Avatar asked May 29 '09 18:05

Jakub Arnold


People also ask

How do you pause a swing in Java?

In Java, we can use TimeUnit. SECONDS. sleep() or Thread. sleep() to pause a program for a few seconds.

What is JDialog modality?

JDialog(Dialog owner, boolean modal) Creates a dialog box with the specified Dialog owner and modality. JDialog(Dialog owner, String title) Creates a modeless dialog box with the specified Dialog owner and title.


2 Answers

Simply use:

setModal(true);

I usually call it from within the constructor of the JDialog.

See the Javadocs on setModal(boolean).
http://java.sun.com/javase/6/docs/api/java/awt/Dialog.html#setModal(boolean)

That will cause execution to block on the current thread until the dialog box closes.

Alternatively, you can use:

setModalityType(Dialog.DEFAULT_MODALITY_TYPE);

It is equivalent to setModal(true) and technically the correct way to do it.

like image 107
jjnguy Avatar answered Oct 20 '22 17:10

jjnguy


See the constructor of JDialog. You can set the modality of this dialog. Setting modal=true will pause your application. You can also use the method setModal.

like image 43
Pierre Avatar answered Oct 20 '22 19:10

Pierre