Documentation

How to Use Modality in Dialogs
Trail: Creating a GUI With JFC/Swing
Lesson: Using Other Swing Features

How to Use Modality in Dialogs

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:


Note : 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.

Four frames to demonstrate different modality types
This figure has been reduced to fit on the page.
Click the image to view it at its natural size.

Try this: 
  1. Click the Launch button to run ModalityDemo using Java™ Web Start (download JDK 7 or later). Alternatively, to compile and run the example yourself, consult the example index.Launches the ModalityDemo application
  2. The following dialog boxes will appear:
    • Book 1 (parent frame)
    • Book 2 (parent frame)
    • Feedback (parent frame)
    • Classics (excluded frame)
  3. Switch to the Book 1 frame and choose the Biography title for the book, then select OK.
  4. The Biography title will be displayed in the title of the dialog box. Enter the name, for example - “John”, into the text field.
  5. Switch to the Book 1 frame and change the title to Funny Tale, then select OK. Since the dialog box for entering the name is modeless, you can easily switch to its parent frame.

    Note : The modeless dialog box title has been changed to Funny Tale.
  6. Select OK in the modeless dialog box.
  7. The Funny tale document-modal dialog box appears.
  8. Type some text in the text field. Notice that it is signed by the name you entered in the modeless dialog box.
  9. Switch to the modeless dialog box and try to change your name. You will not be able to do so, because the document-modal dialog box blocks all windows in its parent hierarchy.
  10. Perform the same sequence of operations (steps 3 - 9) for the Book 2 parent frame.
  11. Try switching to different dialog boxes. You will notice that you can switch either to the Classics frame or to the Feedback frame as well as to the dialog box of either the Book 1 frame or the Book 2 frame.
  12. Switch to the Feedback parent frame. Select Rate Yourself.
  13. The confirmation dialog box will appear. Try switching to different dialog boxes. You are only enabled to switch to the Classics dialog box because the standard confirmation dialog box is an application-modal dialog box and it blocks all windows from the same application. However, you will notice that you can select your favorite classical author in the Classics frame. This frame has been created by using the 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 Modality API

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.

Examples That Use Modality API

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.

Previous page: How to Use Key Bindings
Next page: How to Print Tables