15

I am working on an application which pops up a JOptionPane when a certain action happens. I was just wondering if it was possible that when the JOptionPane does pop up how can you still use the background applications. Currently when the JOptionPane pops up I can't do anything else until I close the JOptionPane.

EDIT

Thanks for the reply guys and the information. Think ill leave this function out of the application cause it looks like it could be more hassle than necessary.

yemyem
  • 155
  • 1
  • 1
  • 6

5 Answers5

11

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

What you can use is the Direct Use method taken from the docs and the setModal method 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
 dialog.show();
 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++) {
    if(options[counter].equals(selectedValue))
    return counter;
 }
 return CLOSED_OPTION;
justkt
  • 14,610
  • 8
  • 42
  • 62
  • 3
    `dialog.show()` does not do what you expect. It just makes the dialog visible. This method will return before the user has clicked "OK". You should instead add an `ActionListener` to inform the app when the user chooses an option. – finnw May 19 '11 at 15:44
  • 1
    @finnw I'm having exactly this problem. How can you wait to return until after the user clicks OK in a *modeless* dialog? – zmb Dec 06 '13 at 16:36
  • 4
    @zmb, if it is modeless then you do not wait for it, you create event listeners for it instead. If you want to wait for it then it should be modal – finnw Dec 06 '13 at 17:02
  • 1
    I put an improved version of this with the suggested event listener in a [new answer](https://stackoverflow.com/a/62792083/1052284) – Mark Jeronimus Jul 08 '20 at 09:52
2

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

    // A non-modal version of JOptionPane.showOptionDialog()
    JOptionPane pane = new JOptionPane(arguments, ...);
    pane.setComponentOrientation(JOptionPane.getRootFrame().getComponentOrientation());
    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");
        dialog.dispose();
    });

    dialog.setModal(false);
    dialog.setVisible(true);
Mark Jeronimus
  • 9,278
  • 3
  • 37
  • 50
1

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

1

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

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

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

Mark Cramer
  • 2,614
  • 5
  • 33
  • 57
yavoh
  • 2,645
  • 5
  • 24
  • 21
  • 1
    `JOptionPane` is a convenience class to pop up pre-fabricated dialogs using simple calls to static methods. I don't think it allows much leeway for the tinkering with modality. – Carl Smotricz Apr 18 '11 at 17:28
  • @CarlSmotricz yes it does. `JOptionPane` extends `JPanel`. It can be created independently of the dialog and have its own `ActionListener`. You can create your own dialog with whatever modality you want and add the `JOptionPane` as a child. – finnw Dec 06 '13 at 17:05
  • Woo hoo! Thanks for correcting me, I didn't know this. There's always yet another thing to be discovered in the Java Library. – Carl Smotricz Dec 09 '13 at 11:46
1

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

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

Carl Smotricz
  • 66,391
  • 18
  • 125
  • 167