Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

MouseListener events problem

 
Ignas Sliuzas
Greenhorn
Posts: 13
Firefox Browser Java Notepad
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
at the start say b=true and a=false;



since I don't really know mouseListener very well my question is why does it do the first if sentence again. All I want to do Is either do one or another, but when second if sentence makes a=true it repeats the mouseClicked() method and spoils all my code. Question is: how do I tackle this problem? Should I do sleep() the thread somewhere? I'm kinda puzzled at this moment. Thanks for your help!

the idea I want to implement is that first click would just make the second if, the second click would do the first if.

I've just checked and it seems it repeats mouseClicked method for around 4-5 times. Is it possible there are hidden calls I'm unaware of?
 
Rob Spoor
Sheriff
Pie
Posts: 20550
57
Chrome Eclipse IDE Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ignas Sliuzas wrote:Should I do sleep() the thread somewhere?

Definitely not. You don't sleep or do any other long-taking processing on the Event Dispatcher Thread (EDT), because that will block the entire EDT until done. In the mean time you don't get mouse events, keyboard events, or even repainting of the GUI. See Concurrency in Swing.
 
Ignas Sliuzas
Greenhorn
Posts: 13
Firefox Browser Java Notepad
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
true, I understood after a while that suggesting sleep wasn't very smart but It seems I have fixed my problem at least for now. I think it had something to do with addMouseEvents() being in the paintComponent() which was a stupid thing to do in the first place. Other than that I have noticed that the second "if" should also have actually been "else if". That seemed to make a difference on another application that I have been testing specifically this part on.
 
Rob Spoor
Sheriff
Pie
Posts: 20550
57
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ignas Sliuzas wrote:I think it had something to do with addMouseEvents() being in the paintComponent() which was a stupid thing to do in the first place.

I can't argue with you there
This would actually add the same event listeners every time the user interface is painted. If you add it twice it gets called twice. If you add it once more it gets called three times, etc.
 
Aromal ayyappan
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if (a){
a=false;
b=true;
}
else if (b){
b=false;
a=true;
}
 
Rob Spoor
Sheriff
Pie
Posts: 20550
57
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If those are the only assignments to a and b and the initial values are different, you can get rid of b completely, as a will always be equal to !b.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic