I want to create in Java Swing a JDialog which, when it's open, its parent window cannot be accessed (just like when you open the file explorer dialog in Microsoft Word). Is there any method in the JDialog class that provides this behaviour?
+2
A:
use JDialog.setModal(true) before setting dialog visible
JDialog yourdialog = ...
yourdialog.setModal(true);
...
yourdialog.setVisible(true);
Jan Wegner
2010-09-14 15:58:24
or use one of the JDialog constructors that defines the modality.
Qwerky
2010-09-14 16:00:45
+1
A:
You have two options:
Use the static methods in JOptionPane
. These will create modal dialogs by default:
Window parentWindow = SwingUtilities.getWindowAncestor(parentPanel);
JOptionPane.showMessageDialog(parentWindow, "Hello, World); // Create modal dialog aligned with parent window.
Create a JDialog
explicitly:
Window parentWindow = SwingUtilities.getWindowAncestor(parentPanel);
JDialog dlg = new JDialog(parentWindow, ModalityType.APPLICATION_MODAL);
The first option is far simpler and I tend to prefer it particularly with modal dialogs.
Adamski
2010-09-14 16:00:53