Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java: What is the property of a JOptionPane object that prevents clicks below itself?

I have created my own JOptionPane using a JDialog, but I can't seem to replicate the behavior of a typical JOptionPane that disallows the user from clicking anywhere but the JOptionPane window.

What property of JOptionPane do I need to replicate with a JDialog so that I can mimic this behavior? (I am aware that JOptionPanes are simply specialized JDialogs, as you can see if look at the JOptionPane class source code).

like image 395
Alexander Mills Avatar asked Jan 13 '23 05:01

Alexander Mills


1 Answers

The Swing API to set the modality of a JDialog is one of the constructors with Dialog.ModalityType as argument.

  • JDialog(Window owner, Dialog.ModalityType modalityType)
  • JDialog(Window owner, String title, Dialog.ModalityType modalityType)
  • JDialog(Window owner, String title, Dialog.ModalityType modalityType, GraphicsConfiguration gc)

To set the modality after creation: java.awt.setModalityType()

To get the modality, use java.awt.Dialog.getModalityType()

The old fashion way is to use a boolean (other constructors).

Sometimes we want to exclude some Windows from the modality with java.awt.setModalExclusionType() (rarely used)

like image 129
Aubin Avatar answered Jan 31 '23 07:01

Aubin