Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Modal JDialog without blocking execution

Is there a way how to use a dialog in Swing which prohibits any gui activity under it but at the same time DOES NOT stop execution on the thread where it was set to visible?

like image 398
ps-aux Avatar asked Jun 03 '14 10:06

ps-aux


People also ask

What is modal in JDialog?

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.

What is the difference between JFrame and JDialog?

JFrame is a normal window with its normal buttons (optionally) and decorations. JDialog on the other side does not have a maximize and minimize buttons and usually are created with JOptionPane static methods, and are better fit to make them modal (they block other components until they are closed).

How do I open JDialog in center of screen?

If you really want to center a JDialog on screen, you can use code like this: // center a jdialog on screen JDialog d = new JDialog(); d. setSize(400, 300); d. setLocationRelativeTo(null); d.

Where is JDialog used in an application?

JDialog is one of the important features of JAVA Swing contributing to interactive desktop-based applications. This is used as a top-level container on which multiple lightweight JAVA swing components can be placed to form a window based application.


2 Answers

Yes it can be done .

dlg.setModal(false);

or

dlg.setModalityType(Dialog.ModalityType.MODELESS);

where dlg is instance of your JDialog .

like image 112
Mr Coder Avatar answered Oct 27 '22 14:10

Mr Coder


The basic idea of a JDialog IS to block the underlying thread until the user reacts to it. If you need to run something on the UI thread which should not be interrupted, consider using an additional worker thread for it. This way, the UI will be blocked by the JDialog, but the underlying process won't.

like image 26
Zahlii Avatar answered Oct 27 '22 14:10

Zahlii