Java Swing How to - Change JDialog from MODELESS to APPLICATION_MODAL








Question

We would like to know how to change JDialog from MODELESS to APPLICATION_MODAL.

Answer

import java.awt.Cursor;
import java.awt.Dialog.ModalityType;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
//from   w  w w. j ava2 s .  co m
import javax.swing.AbstractAction;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;

public class Main {

  public static void main(String[] args) {
    JFrame frame = new JFrame();
    frame.setSize(new Dimension(500, 500));

    JDialog dialog = new JDialog(frame, "Export",
        ModalityType.MODELESS);
    dialog.setSize(300, 300);

    JDialog dialog1 = new JDialog(dialog, "Export",
        ModalityType.APPLICATION_MODAL);
    dialog1.setSize(200, 200);

    frame.add(new JButton(new AbstractAction("Dialog") {
      @Override
      public void actionPerformed(ActionEvent e) {
        frame.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
        dialog.setVisible(true);
        dialog1.setVisible(true);
      }
    }));
    frame.setVisible(true);
  }
}