Java™ SE 6 has resolved modality issues that arose in earlier versions of the platform. The new modality model enables the developer to scope, or limit, a dialog box's modality blocking.
Before proceding with the new modality model, review the following terms:
In Java SE 6 the behavior of both modal and modeless dialog boxes has been changed so that they always appear on top of both not only of their parent windows and of all blocked windows as well.
The following modality types are supported in Java SE 6:
Additionally, you can set up the modality exclusion mode:
Dialog.ModalExclusionType
enum specifies the possible modal exclusion types.The new modality model does not implement a system modality, which blocks all applications (including Java applications) that are displayed on the desktop while a modal dialog box is active.
The ModalityDemo example demonstrates the first three of the four modality types mentioned above.
APPLICATION_EXCLUDE
modality exclusion type, which prevents all top-level windows from being blocked by any application-modal dialog boxes.The following code snippet shows how to create dialog boxes of different modality types:
//The Book 1 parent frame f1 = new JFrame("Book 1 (parent frame)"); ... //The modeless dialog box d2 = new JDialog(f1); ... //The document-modal dialog box d3 = new JDialog(d2, "", Dialog.ModalityType.DOCUMENT_MODAL); ... //The Book2 parent frame f4 = new JFrame("Book 2 (parent frame)"); ... //The modeless dialog box d5 = new JDialog(f4); ... //The document-modality dialog box d6 = new JDialog(d5, "", Dialog.ModalityType.DOCUMENT_MODAL); ... //The excluded frame f7 = new JFrame("Classics (excluded frame)"); f7.setModalityExclusionType(Dialog.ModalExclusionType.APPLICATION_EXCLUDED); ... //The Feedback parent frame and Confirm Dialog box f8 = new JFrame("Feedback (parent frame)"); ... JButton b8 = new JButton("Rate yourself"); b8.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { JOptionPane.showConfirmationDialog(null, "I really like my book", "Question (application-modal dialog)", JOptionPane.Yes_NO_OPTION, JOptionPane.QUESTION_MESSAGE); } });
Find the demo's complete code in the
ModalityDemo.java
file.
In Java SE 6 you can create a document-modal dialog box without a parent. Because the Dialog
class is a subclass of the Window
class, a Dialog
instance automatically becomes the root of the document if it has no owner. Thus, if such a dialog box is document-modal, its scope of blocking is empty, and it behaves as if it were a modeless dialog box.
The JDialog
class constructors enable you to create dialog boxes of various modality types.
Constructor | Purpose |
---|---|
JDialog(Dialog owner) | Creates a modeless dialog box with the specified Dialog owner but without a title. |
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. |
JDialog(Dialog owner, String title, boolean modal) | Creates a dialog box with the specified Dialog owner, title, and modality. |
JDialog(Dialog owner, String title, boolean modal, GraphicsConfiguration gc) | Creates a dialog box with the specified Dialog owner, title, modality, and graphics configuration. |
JDialog(Frame owner) | Creates a modeless dialog box without a title with the specified Frame owner. If the value for the owner is null, a shared, hidden frame will be set as the owner of the dialog box. |
JDialog(Window owner, String title, Dialog.ModalityType modalityType) | Creates a dialog box with the specified Window owner, title, and modality. |
The following table lists methods inherited from the
java.awt.Dialog
class.
Method | Purpose |
---|---|
getModalityType | Returns the modality type for this dialog box. |
setModalityType | Sets the modality type for this dialog box. See
ModalityType for possible modality types. If the given modality type is not supported, then the MODELESS type is used. To ensure that the modality type has been set, call the getModalityType() method after calling this method. |
The following table lists the example that uses modality in dialogs.
Example | Where Described | Notes |
---|---|---|
ModalityDemo |
This section | Creates dialog boxes of different modality types, demonstrates scope blocking for those types. |