JOptionPane.showMessage call inside run method of a Thread...

I am doing this

final Thread calcThread =new Thread(){

publicvoid run(){

try{

md5=MessageDigest.getInstance(algorithm.getSelectedItem().toString());

if(userText.getText().equals(""))

{

if(textMode)

{JOptionPane.showMessageDialog(me,"Please fill in the text to be hashed...","Error",JOptionPane.ERROR_MESSAGE);}

else

{JOptionPane.showMessageDialog(me,"Please select the File to be hashed...","Error",JOptionPane.ERROR_MESSAGE);}

}

When i am getting into the condition where i have to show the JOptionPane message my code is getting stuck!

I am not getting any exception, or my exception handling isn't that good...

Please can you help sorting out this small problem, i am sure i am missing something very critical and very minute at the same time :)

[1473 byte] By [DhruvaSagara] at [2007-10-3 10:24:51]
«« TMP file
»» TMP file
# 1
Please help me people....
DhruvaSagara at 2007-7-15 5:46:45 > top of Java-index,Desktop,Core GUI APIs...
# 2

SwingUtilities.invokeLater(new Runnable()

{

public void run()

{

// Try here with your code

}

});

Prashant_SDNa at 2007-7-15 5:46:45 > top of Java-index,Desktop,Core GUI APIs...
# 3

More than a solution, i wanted to understand the reason behind the strange behavior. Perhaps it's not so strange, just that i dont know or understand why it happens like that i say it's strange :D

Well please help me identify as to why exactly the thing might be occuring, where is the code going wrong?

DhruvaSagara at 2007-7-15 5:46:45 > top of Java-index,Desktop,Core GUI APIs...
# 4

please help me understand this, that is the sole purpose of my queries, i can always do without threading...

And please also along with my problem help me understand a bit more regarding this SwingUtilities.invokeLater() method, what is it's use and what exactly does it do and how does it effect the execution, running of a program?

I hope to receive a good response from you people in this regards.

Thanks.

DhruvaSagara at 2007-7-15 5:46:45 > top of Java-index,Desktop,Core GUI APIs...
# 5

hi!

you better give a console print after md5=MessageDigest.getInstance(algorithm.getSelectedItem().toString());

to check what are you getting for userText.getText().

i think that value is not ""

so none of the condition is satisfied..

:)

Aniruddha-Herea at 2007-7-15 5:46:45 > top of Java-index,Desktop,Core GUI APIs...
# 6
That i know!But i want that if the user presses the Caclulate button without entering any text then it gives an error through JOptionPane.showMessageDialog() saying that please enter the text first...This doesn't make things clear though...
DhruvaSagara at 2007-7-15 5:46:45 > top of Java-index,Desktop,Core GUI APIs...
# 7
Is this a bug or what?Please tell me something, how can no one know this or have come accross a similar thing?
DhruvaSagara at 2007-7-15 5:46:45 > top of Java-index,Desktop,Core GUI APIs...
# 8

i think you need to create a Short, Self Contained, Compilable and Executable, Example Program that demonstrates the incorrect behaviour. then it can be said that it is a bug of JVM or your code.

And don't forget to use the Code Formatting Tags so the code retains its original formatting.

http://homepage1.nifty.com/algafield/sscce.html

http://forum.java.sun.com/help.jspa?sec=formatting

Aniruddha-Herea at 2007-7-15 5:46:45 > top of Java-index,Desktop,Core GUI APIs...
# 9

The option pane's dialog requires EDT thresd to be shown. It's modal that's hy it locks your thread but painitng isn't possible there. So it looks like hanging.

You have to call it by SwingUtilities.invokeAndWait() it menas current thread will be stopped and EDT will work (show your options dialog) then when the dialog is closed your thread will continue working.

regards,

Stas

P.S. invokeLater should be applied to actions from EDT. It just put a new call in events queue. The cole (Runnable) will be executed in turn.

StanislavLa at 2007-7-15 5:46:45 > top of Java-index,Desktop,Core GUI APIs...
# 10

Thank you very much, that was indeed a very good explanation and after i actually implemented it in my code, i see the effects pretty clearly.

Thanks a million for that explanation, it really widened my view and knowledge in swings and has indeed helped me learn much more now. :)

Thanks & Regards.

Dhruva Sagar

DhruvaSagara at 2007-7-15 5:46:45 > top of Java-index,Desktop,Core GUI APIs...