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

后端 未结 5 1493
一向
一向 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: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);
    

提交回复
热议问题