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?
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.
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.
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.