Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Thread.sleep problem (change image) Java

 
Babis Papadopoulos
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Pie
Posts: 20945
31
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Pie
Posts: 20945
31
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Pie
Posts: 20945
31
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Pie
Posts: 20945
31
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic