aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes MouseListener events problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "MouseListener events problem" Watch "MouseListener events problem" New topic
Author

MouseListener events problem

Ignas Sliuzas
Greenhorn

Joined: Oct 15, 2009
Posts: 13

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

Joined: Oct 27, 2005
Posts: 19692
    
  20

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Ignas Sliuzas
Greenhorn

Joined: Oct 15, 2009
Posts: 13

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

Joined: Oct 27, 2005
Posts: 19692
    
  20

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

Joined: Jan 03, 2012
Posts: 3
if (a){
a=false;
b=true;
}
else if (b){
b=false;
a=true;
}
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19692
    
  20

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: MouseListener events problem