• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

don't want to register button click if you move cursor off of button

 
Saloon Keeper
Posts: 10750
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I've got a Swing app with a number of buttons. Each button has its own MouseListener extending a MouseAdapter. I've only implemented the mouseReleased() method. My think was that if the user clicked on a button and with the button still held down they could change their mind by sliding the cursor off of the button and then releasing it. Doesn't work though. It remembers which button was under the cursor when the mouse was pressed.

I tried testing event.getSource() compared to button but that still shows that they both refer to the same button.

Any suggestions?

This question pertains to my Game of Mines program in the game development forum.
 
Carey Brown
Saloon Keeper
Posts: 10750
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hmmm. Came up with this eventually. Seems to work. Was there something cleaner I could have done?

 
Marshal
Posts: 79273
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I haven't read your game of mines thread, but why would you want a mouse listener rather than an action listener?
 
Bartender
Posts: 5466
212
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
With a MouseListener you can test for the events Entered, Exited et cetera. But Carey might have used the mouseClicked-method, or indeed an ActionListener. Here is a short demo that shows the different events (left the imports out):
 
Rancher
Posts: 3324
32
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

My think was that if the user clicked on a button and with the button still held down they could change their mind by sliding the cursor off of the button and then releasing it


That is why you should be using an ActionListener. The ActionEvent is only generated if you have a mousePressed/mouseReleased event on the same component. The two events can be anywhere on the component. So this will allow you to move the mouse off the button before generating the released event.

When you use a MouseListener a mouseClicked event is only generated when the mousePressed/mouseReleased event is generated for the same point on the component. So the clicked event is NOT generated even if the mouse moves by a single pixel.




 
Author
Posts: 986
3
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To expand on what Mr. Camick said, this is controlled by the ButtonModel. If you read the text at the top of the ButtonModel javadoc (at, for example, https://docs.oracle.com/javase/8/docs/api/javax/swing/ButtonModel.html ) it says the following:

Pressing the mouse on top of a button makes the model both armed and pressed. As long as the mouse remains down, the model remains pressed, even if the mouse moves outside the button. On the contrary, the model is only armed while the mouse remains pressed within the bounds of the button (it can move in or out of the button, but the model is only armed during the portion of time spent within the button). A button is triggered, and an ActionEvent is fired, when the mouse is released while the model is armed - meaning when it is released over top of the button after the mouse has previously been pressed on that button (and not already released).



So, if you do the ActionListener thing, you should be getting the behavior you say you want, unless someone has messed with the ButtonModel.
 
Saloon Keeper
Posts: 27819
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Condensing and confirming. It's standard GUI practice that releasing a mouse button if you've moved the pointer off the burron control cancels a mouseClick event. However, raw mouse events will still fire. For one thing, clicking, holding, and moving is how mouse dragging is done.

So if you want mouseClick action, listen for mouseClick, not for mouse events.
 
Rancher
Posts: 326
14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Although a month late to the party just using ActionListener was what was in my head while reading the OP - as I used it back when java7-ea came out (a few before final java7.0 release) for my battle ships games. I also used mouse events for the initial positioning before the start of the game - but that was rather bad code which I don't really know how I would had improved even today all these years later.
 
Carey Brown
Saloon Keeper
Posts: 10750
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Matthew Bendford wrote:Although a month late to the party just using ActionListener was what was in my head while reading the OP - as I used it back when java7-ea came out (a few before final java7.0 release) for my battle ships games. I also used mouse events for the initial positioning before the start of the game - but that was rather bad code which I don't really know how I would had improved even today all these years later.

By the time "ActionListener" was mentioned I already had something working. I'm living with my code for now just to see how I feel about the behavior. If anything doesn't feel right out it goes and in goes ActionListener which would probably be cleaner anyway. Thanks.
 
Rob Camick
Rancher
Posts: 3324
32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So you ask a question and then 20 minutes later repost stating you have a solution.

But, you also ask if there is a cleaner way.

Multiple people respond stating the cleaner way is to NOT reinvent the wheel and to use the API as it was designed by using an ActionListener.

You didn't respond to the suggestion. Why would you ask for a cleaner way if you have no intention of using it?

We could have better spent out time helping others who do intend to at least try the suggestion before deciding on a final implementation.
 
Carey Brown
Saloon Keeper
Posts: 10750
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm sorry if I've offended you. I posted 4 weeks ago and my post included a solution I was already in the middle of. I was not aware of the use of ActionListener and should have made that clearer, and I may as yet return to that. When there's  not much activity on a topic for weeks I assume that the people who were interested already posted their comments.
reply
    Bookmark Topic Watch Topic
  • New Topic