Improved Dialog Modality : Modality « Swing « Java Tutorial






Mustang introduces different types of modality, in which you no longer use setModal but the new method setModalityType in the Dialog class to make a dialog modal.

Dialog.ModalityType is an enum in the java.awt package.

Its values are as follows:

  1. APPLICATION_MODAL. Blocks all windows in the same application except those with the modal dialog as their owner.
  2. DOCUMENT_MODAL. Makes all windows from the same document inaccessible, except those from the modal dialog's child hierarchy.
  3. MODELESS. Does not block any other windows.
  4. TOOLKIT_MODAL. Makes all windows from the same toolkit inaccessible, except those from the modal dialog's child hierarchy.
import java.awt.Dialog;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;

public class ApplicationModalDialogDemo {

  public static void main(String[] args) {
    final JFrame parent1 = new JFrame("Parent Frame 1");
    parent1.setLayout(new FlowLayout());
    parent1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JButton button = new JButton("Application modal dialog");
    button.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        JDialog dialog = new JDialog(parent1, "Application-Modal Dialog",
            Dialog.ModalityType.APPLICATION_MODAL);
        dialog.setBounds(200, 150, 200, 150);
        dialog.setVisible(true);
      }
    });
    parent1.add(button);
    parent1.setBounds(100, 100, 200, 150);
    parent1.setVisible(true);

    JFrame parent2 = new JFrame("Parent Frame 2");
    parent2.setBounds(500, 100, 200, 150);
    parent2.setVisible(true);
  }
}








14.75.Modality
14.75.1.Four new AWT modality models introduced with the Java Mustang release.
14.75.2.Improved Dialog Modality
14.75.3.Dialog.ModalityType.DOCUMENT_MODAL
14.75.4.Modeless Dialog
14.75.5.Using modality exclusion
14.75.6.Demonstrating Modality Types