How many types of JDialog boxes can be created in Java?

JavaObject Oriented ProgrammingProgrammingSwing

A JDialog is a subclass of Dialog class and it does not hold minimize and maximize buttons at the top right corner of the window. We can create two types of JDialog boxes.in Java

  • Modal Dialog
  • Non-Modal Dialog

Modal JDialog

In Java, When a modal dialog window is active, all the user inputs are directed to it and the other parts of the application are inaccessible until this model dialog is closed.

Non-Modal JDialog

In Java, When a non-modal dialog window is active, the other parts of the application are still accessible as normal and inputs can be directed to them, while this non-modal dialog window doesn't need to be closed.

Example

import javax.swing.*;
import java.awt.*;
import java.awt.Dialog.ModalityType;
public class Modal_NonModal_Dialog {
   public static void main(String[] args) {
      JFrame frame = new JFrame();
      frame.setTitle("Modal and Non-Modal Dialog");
      frame.setSize(350, 300);
      frame.setLocationRelativeTo(null);
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.setVisible(true);
      // modal dialog
      JDialog nonModalDialog = new JDialog(frame, "Non-Modal Dialog", ModalityType.MODELESS);
      nonModalDialog.setSize(300, 250);
      nonModalDialog.setLocationRelativeTo(null);
      nonModalDialog.setVisible(true);
      // non-modal dialog
      JDialog modalDialog = new JDialog(frame, "Modal Dialog", ModalityType.APPLICATION_MODAL);
      modalDialog.setSize(300, 250);
      modalDialog.setLocationRelativeTo(null);
      modalDialog.setVisible(true);
   }
}

Output

raja
Published on 08-Jul-2019 19:46:39
Advertisements