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 :)
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?
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.
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..
:)
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
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.
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