Search code examples
javaswingfile-iojoptionpane

JOptionPane.showMessageDialog doesn't show up after reading an InputStream. [Java]


Here is the piece of code which I think is causing the problem.

while((is.read(bytes)) != -1)
            {   
                fos.write(bytes);
            }       

            JOptionPane.showMessageDialog(null,"File Received.","Complete.",JOptionPane.INFORMATION_MESSAGE);
            //System.out.println("File Received.");

Now when the control comes to the JOptionPane statement nothing shows up and the program won't even ends up. I had to end it from the task manager manually. When I tried commenting out the JOptionPane statement and used the console method i.e. System.out.println() to show the message it worked and the program ended normally. I don't understand why this JOptionPane is causing this problem. I am stuck here. Help would be appreciated. Thanks in anticipation.


Solution

  • You have to close the stream once you finish. It is working. When fos.close() is executed it notifies the EDT and message is shown. In case of System.out.print, it is not in the EDT so it printed once write operation is completed.

    while((is.read(bytes)) != -1)
                {   
                    fos.write(bytes);
                }       
                fos.close();
                JOptionPane.showMessageDialog(null,"File Received.","Complete.",JOptionPane.INFORMATION_MESSAGE);
                //System.out.println("File Received.");