Please note, this is a STATIC archive of website www.tutorialspoint.com from 11 May 2019, cach3.com does not collect or store any user information, there is no "phishing" involved.
Tutorialspoint

1 Answer
karthikeya Boyini

MODELESS Type

The following is an example to set JDialog with Modality type MODELESS −

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.MODELESS);
      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 can close both the dialog at any time since it is Modeless −

Model Dialog

The following is an example for model Dialog −

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

We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy.