[ACCEPTED]-Can I use a Java JOptionPane in a non-modal way?-joptionpane
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
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;
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.
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.
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.
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, ...);
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);
More Related questions
We use cookies to improve the performance of the site. By staying on our site, you agree to the terms of use of cookies.