Showing “JOptionPane.showMessageDialog” without stopping flow of execution

后端 未结 3 1714
说谎
说谎 2021-02-15 16:48

I\'m currently working on a project that\'s getting more complex than I thought it would be originally. What I\'m aiming to do right now is show a message dialog without halting

相关标签:
3条回答
  • 2021-02-15 17:26

    try this one:

      EventQueue.invokeLater(new Runnable(){
                            @Override
                            public void run() {
                         JOptionPane op = new JOptionPane("Hi..",JOptionPane.INFORMATION_MESSAGE);
                         JDialog dialog = op.createDialog("Break");
                         dialog.setAlwaysOnTop(true);
                         dialog.setModal(true);
                         dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);      
                         dialog.setVisible(true);
                            }
                        });
    
    0 讨论(0)
  • 2021-02-15 17:44

    According to the docs:

    JOptionPane creates JDialogs that are modal. To create a non-modal Dialog, you must use the JDialog class directly.

    The link above shows some examples of creating dialog boxes.


    One other option is to start the JOptionPane in its own thread something like this:

      Thread t = new Thread(new Runnable(){
            public void run(){
                JOptionPane.showMessageDialog(null, "Hello");
            }
        });
      t.start();
    

    That way the main thread of your program continues even though the modal dialog is up.

    0 讨论(0)
  • 2021-02-15 17:44

    You could just start a separate Runnable to display the dialog and handle the response.

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