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

后端 未结 5 1481
一向
一向 2020-11-30 10:03

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 c

5条回答
  •  情深已故
    2020-11-30 10:45

    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;
    

提交回复
热议问题