Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Showing "JOptionPane.showMessageDialog" without stopping flow of execution

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 the execution of the main thread in the program. Right now, I'm using:

JOptionPane.showMessageDialog(null, message, "Received Message", JOptionPane.INFORMATION_MESSAGE);

But this pauses everything else in the main thread so it won't show multiple dialogs at a time, just on after the other. Could this m=be as simple as creating a new JFrame instead of using the JOptionPane?

like image 767
Brandon Avatar asked Mar 26 '11 03:03

Brandon


People also ask

Which method of the JOptionPane class is used display information on the Dialogbox?

Creating a standard dialog is as simple as applying one the following methods to JOptionPane: showMessageDialog or showOptionDialog. The showMessageDialog method creates a basic one-button dialog box.

What is the return type from invoking JOptionPane showInputDialog () method?

JOptionPane. showInputDialog() will return the string the user has entered if the user hits ok, and returns null otherwise.

What does JOptionPane showConfirmDialog return?

What is the return type from invoking JOptionPane showConfirmDialog () method? It will return an int which represents which button was pressed.


1 Answers

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.

like image 141
Vincent Ramdhanie Avatar answered Oct 20 '22 06:10

Vincent Ramdhanie