What happens when JDialog is set with Modality type MODELESS in Java


Modeless dialog boxes are on the screen and are available for use. 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:

Updated on: 30-Jul-2019

59 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements