What happens when JDialog is set with Modality type APPLICATION_MODAL



The JDialog Modality type APPLICATION_MODAL blocks all top-level windows and it has restrictions. The following is an example to set JDialog with Modality type APPLICATION_MODAL:

Example

import java.awt.Cursor;
import java.awt.Dialog.ModalityType;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import javax.swing.AbstractAction;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
public class SwingDemo {
   public static void main(String[] args) {
      JFrame frame = new JFrame();
      frame.setSize(new Dimension(600, 400));
      JDialog dialog = new JDialog(frame, "New",ModalityType.APPLICATION_MODAL);
      dialog.setSize(300, 300);
      frame.add(new JButton(new AbstractAction("Click to generate") {
         @Override
         public void actionPerformed(ActionEvent e) {
            frame.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
            dialog.setVisible(true);
         }
      }));
      frame.setVisible(true);
   }
}

Output

Now, click on it to generate a new Dailog. You cannot close both the dialog at any time since it isn’t Modeless. You have to first close the new Dialog, the you will be able to close the first:



Advertisements