Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I use a Java JOptionPane in a non-modal way?

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.

like image 491
yemyem Avatar asked Apr 18 '11 17:04

yemyem


People also ask

Are all dialogs created by JOptionPane modal?

All the dialogs that JOptionPane provides are modal. To create a non-modal dialog, you must use the JDialog class directly. class. It adds to Dialog a root pane and support for a default close operation.

Why do we use JOptionPane in Java?

JOptionPane makes it easy to pop up a standard dialog box that prompts users for a value or informs them of something. For information about using JOptionPane , see How to Make Dialogs, a section in The Java Tutorial. Asks a confirming question, like yes/no/cancel.

What is JOptionPane used for?

Java AWT Programming Project The JOptionPane is used instead of JDialog to minimize the complexity of the code. The JOptionPane displays the dialog boxes with one of the four standard icons (question, information, warning, and error) or the custom icons specified by the user.

Is JOptionPane a GUI?

JOptionPane is a nice way to throw together a few dialog boxes and get a GUI, but it doesn't give us the flexibility we really want. But with power and flexibility come complexity. The basic class we start with is a JFrame.

Is there a modal dialog for joptionpane in Java?

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.

What is the use of joptionpane in Java?

The JOptionPane is used instead of JDialog to minimize the complexity of the code. The JOptionPane displays the dialog boxes with one of the four standard icons (question, information, warning, and error) or the custom icons specified by the user. JOptionPane class is used to display four types of dialog boxes

What is the use of joptionpane dialog box?

These dialog boxes are used to display information or get input from the user. The JOptionPane class inherits JComponent class. It is used to create a JOptionPane with a test message.

What is the difference between modal and non-modal dialogs?

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.


1 Answers

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;
like image 94
justkt Avatar answered Oct 20 '22 15:10

justkt