permaculture playing cards*
The moose likes Threads and Synchronization and the fly likes Thread.sleep problem (change image) Java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Thread.sleep problem (change image) Java" Watch "Thread.sleep problem (change image) Java" New topic
Author

Thread.sleep problem (change image) Java

Babis Papadopoulos
Greenhorn

Joined: Dec 11, 2012
Posts: 17
Hi and sorry for my English!

I have two jlabels with an image.. I try to change the image in a label when the other is clicked.. (such as a game).. I have a problem..When i write the Thread.sleep the image is not change..See the code:



Run it and first click the first label.. After click the second label and the first will change the image in 1 second but the seconf label changes the images NEVER!!

Help me!
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

Well, first of all using Thread.sleep in the Swing event thread is a bad idea. It just locks the GUI. You should go through the Swing concurrency tutorial to find out how to deal with that sort of thing.

And I don't understand why you are using a MouseListener to handle something which you would normally use an ordinary ActionListener for. That seems like a complicated and confusing way of handling a simple action.
Babis Papadopoulos
Greenhorn

Joined: Dec 11, 2012
Posts: 17
Maybe I must use a javax.swing.Timer task but I can't do it to work good.. If you can help me, please...
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

You posted that only 12 minutes after I suggested you should read that tutorial. I would expect it to take a lot longer than 12 minutes to read and understand it, so I'm going to suggest again that you read it.

And please read our FAQ entry ShowSomeEffort which explains what I'm trying to say here.
Babis Papadopoulos
Greenhorn

Joined: Dec 11, 2012
Posts: 17
ok the threads is not suggested for swings.. I write this:


but i have this problem :constructor Timer in class Timer cannot be applied to given types; required: int,ActionListener found: no arguments reason: actual and formal argument lists differ in length –
and I don't know how to fix it
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

I don't understand that code either. The only constructor for javax.swing.Timer is this one:



But your code contains



Which is what the error message is complaining about -- the Timer constructor you tried to use.

And Timer doesn't even have a method named "schedule". Did you copy that code from somewhere?
Babis Papadopoulos
Greenhorn

Joined: Dec 11, 2012
Posts: 17
Yes the general framework but I don't remember from whre..
Anyway.. I try to find something like this:

to put it in the block of if in mouseReleased.. but I can't write a correct code
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

The second parameter of the constructor should be an ActionListener. You use an anonymous inner class for that, exactly as you did with the TimerTask in your posted code.
Babis Papadopoulos
Greenhorn

Joined: Dec 11, 2012
Posts: 17
I write this but it has the same problem (the label is not change the image)


The timer.setRepeats(false); timer.start(); I saw from tutorials..
Babis Papadopoulos
Greenhorn

Joined: Dec 11, 2012
Posts: 17
We can write it with Threads!!


@Override
public void mouseReleased(MouseEvent e) {
if(isClicked){
l2.setIcon(new ImageIcon(getClass().getResource("image1.png")));
new Thread() {
@Override
public void run() {
l2.setIcon(new ImageIcon(getClass().getResource("image1.png")));
try {sleep(1000);} catch (InterruptedException ex) {Logger.getLogger(Game.class.getName()).log(Level.SEVERE, null, ex);}
l2.setIcon(new ImageIcon(getClass().getResource("image2.png")));
l1.setIcon(new ImageIcon(getClass().getResource("image2.png")));
isClicked2 = false;
isClicked = false;
}
}.start();

}
else{
l2.setIcon(new ImageIcon(getClass().getResource("lion.jpg")));
isClicked2 = true;
}

It works perfect!
 
 
subject: Thread.sleep problem (change image) Java
 
Similar Threads
Display an image for a set time
no mouseClicked when TransferHandler set for JLabel?
Clean way to change between mutlible pictures
four in a row rmi game
how to paint some shape over another JComponent