Change JButtons ImageIcon for 0,5seconds when pressing it.
3 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Gustaf_Haag
Posted On:   Wednesday, December 8, 2004 08:58 AM

public void change(int number) { try {Thread.sleep(300);} catch (InterruptedException e) {} mybutton[number].setIcon(down[number]); try {Thread.sleep(500);} catch (InterruptedException e) {} mybutton[number].setIcon(up[number]); } When i call this method from my Contructor it works fine, but when i call it from Actionperformed, it won't sleep! I found out that when i call it and it works the thread's name is "Thread[main,5,main]", but when i call it from actionperformed it's "Thread[AWT-EventQueue-o,6,main]". What i want to do: Have one    More>>
public void change(int number)

{

try {Thread.sleep(300);} catch (InterruptedException e) {}

mybutton[number].setIcon(down[number]);

try {Thread.sleep(500);} catch (InterruptedException e) {}

mybutton[number].setIcon(up[number]);

}




When i call this method from my Contructor it works fine, but when i call it
from Actionperformed, it won't sleep!



I found out that when i call it and it works the thread's name is
"Thread[main,5,main]", but when i call it from actionperformed it's
"Thread[AWT-EventQueue-o,6,main]".



What i want to do:

Have one of my JButtons change ImageIcon for 0,5 seconds
when i press on it!



The Problem:

The thread won't sleep when i call it from actionperformed.



//Gustaf



Ps. I have no such things like "Run()" or "new Thread" or something like that in my code.    <<Less

Re: Change JButtons ImageIcon for 0,5seconds when pressing it.

Posted By:   Craig_Wood  
Posted On:   Tuesday, December 21, 2004 11:42 PM


...
public void actionPerformed(ActionEvent e)
{
// when you are ready to call the change method
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
change(myNumber);
}
});
}

Re: Change JButtons ImageIcon for 0,5seconds when pressing it.

Posted By:   Anonymous  
Posted On:   Friday, December 17, 2004 02:43 AM

Do a repaint() on the mybutton after each setIcon ... or maybe an invalidate(). Such things usually get it to work.

Regards
Harald M.

Re: Change JButtons ImageIcon for 0,5seconds when pressing it.

Posted By:   luke_raeside  
Posted On:   Thursday, December 9, 2004 07:19 AM

I recall somewhere in my memory that the action events have a single thread of execution and that you should use the SwingUtilities for threads in event handlers....sorry that's all I can remember but maybe it'll point in the right direction...
About | Sitemap | Contact