[ACCEPTED]-Can I use a Java JOptionPane in a non-modal way?-joptionpane

Accepted answer
Score: 11

The documentation explicitly states that 4 all dialogs are modal when created through the showXXXDialog 3 methods.

What you can use is the Direct 2 Use method taken from the docs and the setModal method 1 that JDialog inherits from Dialog:

 JOptionPane pane = new JOptionPane(arguments);
 // Configure via set methods
 JDialog dialog = pane.createDialog(parentComponent, title);
 // the line below is added to the example from the docs
 dialog.setModal(false); // this says not to block background components
 Object selectedValue = pane.getValue();
 if(selectedValue == null)
   return CLOSED_OPTION;
 //If there is not an array of option buttons:
 if(options == null) {
   if(selectedValue instanceof Integer)
      return ((Integer)selectedValue).intValue();
   return CLOSED_OPTION;
 //If there is an array of option buttons:
 for(int counter = 0, maxCounter = options.length;
    counter < maxCounter; counter++) {
    return counter;
Score: 2

You should be able to get more information 9 here: http://download.oracle.com/javase/tutorial/uiswing/components/dialog.html

A Dialog can be modal. When a modal Dialog 8 is visible, it blocks user input to all 7 other windows in the program. JOptionPane 6 creates JDialogs that are modal. To create 5 a non-modal Dialog, you must use the JDialog 4 class directly.

Starting with JDK6, you 3 can modify Dialog window modality behavior 2 using the new Modality API. See The New Modality API for 1 details.

Score: 1

Within your Java application, I think you're 7 out of luck: I haven't checked, but I think 6 that JOptionPane's showXXXDialog methods 5 pop up a so-called modal dialog that keeps 4 the rest of the GUI from the same JVM inactive.

However, Java 3 doesn't have any foreground-grabbing super 2 powers: You should still be able to Alt-Tab 1 to other (non-Java) applications.

Score: 1

This easy tweak worked for me (1.6+). Replaced 6 the showXXXDialog with four lines of code 5 to: (1) create a JOptionPane object (2) call 4 its createDialog() method to get a JDialog 3 object (3) set the modality type of the 2 JDialog object to modeless (4) set the visibility 1 of the JDialog to true.

Score: 1

An improved version of @justkt's answer, with the 1 event listener suggested in its comments.

    // A non-modal version of JOptionPane.showOptionDialog()
    JOptionPane pane = new JOptionPane(arguments, ...);
    JDialog dialog = pane.createDialog((Component)parent, "title");

    pane.addPropertyChangeListener(JOptionPane.VALUE_PROPERTY, ignored -> {
        Object selectedValue = pane.getValue();
        System.out.print("Out of " + Arrays.toString(pane.getOptions()) + ", ");
        System.out.println(selectedValue + " was selected");


More Related questions