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

后端 未结 5 1466
一向
一向 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:41

    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.

    0 讨论(0)
  • 2020-11-30 10:43

    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);
    
    0 讨论(0)
  • 2020-11-30 10:44

    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.

    0 讨论(0)
  • 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;
    
    0 讨论(0)
  • 2020-11-30 10:50

    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.

    0 讨论(0)
提交回复
热议问题