Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Detection of double click  RSS feed

 
Bob Sale
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Experimenting with a graphics application in which I want to draw an object (e.g., a line or rect) on top of an image and then use mouse clicks to affect the object (e.g., single click to erase or double click to run other code. The trouble is that getClickCount() only seems to respond to the first half of a double click (i.e., gives a value of 1 instead of 2). I have checked a lot of resources and many show code such as if(event.getClickCount() ==2) then do something, but this sure doesn't work in my example.
Can anyone help me out here? Thanks.

 
Paul Clapham
Sheriff
Posts: 22185
38
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I had a quick look at the MouseListener tutorial and I noticed that they used the clickCount() method in mouseReleased, rather than in mouseClicked, as you did. I haven't used MouseListeners myself but my explanation based on that small amount of data would be that mouseClicked is called immediately upon the first of the two clicks happening.
 
Bob Sale
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the suggestion. I tried it and got the same results: getClickCount returns 1 instead of 2.

I had already seen the tutorial reference that you cited and note that getClickCount is used in the mousePressed, mouseReleased and mouseClicked methods. Were you referring to another reference?
 
Paul Clapham
Sheriff
Posts: 22185
38
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah, that's the one. And you're right about where the clickCount() method is called. But when I ran the demo on that page and tried double-clicking I noticed that each of the mouse listener methods were called, once for the first click (with click count = 1) and then again for the second click (with click count = 2). Try it yourself and you'll see what I mean.

So you're right that they get called with click count = 1, but if you've designed your application properly that shouldn't matter, since they will get called later with click count = 2.
 
Rob Camick
Ranch Hand
Posts: 2754
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your code works fine for me. Hint don't use a JOptionPane for displaying messges. You will never get a double click since your single click code will display the option pane and the second mouse click will be on the option pane.

Also, DON'T override JLabel for custom painting. Extend JPanel or JComponent.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!