0

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.

mKorbel
  • 109,525
  • 20
  • 134
  • 319
Eesa
  • 2,762
  • 2
  • 32
  • 51
  • 2
    [event dispatch thread](http://stackoverflow.com/a/12643642/714968), more in Oracle tutorial - Concurency in Swing – mKorbel Nov 21 '14 at 11:00

1 Answers1

1

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.");
Sridhar
  • 1,832
  • 3
  • 23
  • 44
  • Thanks man it worked. Can you tell what is EDT or provide me the link to some tutorial. Thanks in anticipation. – Eesa Nov 21 '14 at 15:07
  • @essaji: Start with [*Concurrency in Swing*](https://docs.oracle.com/javase/tutorial/uiswing/concurrency/). – Catalina Island Nov 21 '14 at 16:36