Java Swing How to - Create Modeless and model Dialog from JFrame








Question

We would like to know how to create Modeless and model Dialog from JFrame.

Answer

// w  w  w  .j  a  v  a 2 s . c  om
import java.awt.Dialog.ModalityType;
import java.awt.Dimension;

import javax.swing.Box;
import javax.swing.JDialog;
import javax.swing.JFrame;

public class Main {
  public static void main(String[] args) {
    JFrame frame1 = new JFrame();
    frame1.setExtendedState(JFrame.MAXIMIZED_BOTH);
    frame1.setUndecorated(true);
    frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame1.setVisible(true);

    JDialog nonModalDialog = new JDialog(frame1, "Non-Modal Dialog",
        ModalityType.MODELESS);
    nonModalDialog.add(Box.createRigidArea(new Dimension(200, 200)));
    nonModalDialog.pack();
    nonModalDialog.setVisible(true);

    JDialog modalDialog = new JDialog(frame1, "Modal Dialog",
        ModalityType.APPLICATION_MODAL);
    modalDialog.add(Box.createRigidArea(new Dimension(200, 200)));
    modalDialog.pack();
    modalDialog.setVisible(true);

  }
}