Results 1 to 10 of 10
Like Tree3Likes
  • 1 Post By DarrylBurke
  • 1 Post By Fubarable
  • 1 Post By DarrylBurke

Thread: Long Press Button

  1. #1
    Ksammie01 is offline Member
    Join Date
    Apr 2012
    Posts
    8
    Rep Power
    0

    Post Long Press Button

    Hi all.
    i am working on this class project, its tool demonstrating automated car driving.there is a speedometer and an acceleration and deceleration button.
    the problem is, the speedometer is supposed to show an increase/decrease in speed only if the button is long pressed..and what i have always done, is to reaction only if a button is clicked once. so how do i do that?

  2. #2
    wsaryada is offline Senior Member
    Join Date
    Jun 2007
    Location
    Bali, Indonesia
    Posts
    760
    Rep Power
    8

    Default Re: Long Press Button

    You can add status variable to remember the status of the button. When the button was clicked change the status to pressed = true, and when the click happen again change the status to pressed = false. Then based on this status you can increase or decrease your speedometer.

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,256
    Rep Power
    19

    Default Re: Long Press Button

    Moved from New to Java.

    @wsaryada: I interpret the question as being about pressing and holding a button, not clicking and then clicking again.

    There's an approach I suggested on another forum, but on rethinking it, you could probably use a PropertyChangeListener to better effect. Can't test that right now as my power just went out and I'm running on a UPS so have to shut down.

    Let us know what you tried.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  4. #4
    Ksammie01 is offline Member
    Join Date
    Apr 2012
    Posts
    8
    Rep Power
    0

    Default Re: Long Press Button

    @wsaryada, its about pressing and holding a button, like DarrylBurke's interpretation.
    @DarrylBurke, i have seen the approach on the other forum , but am yet to try it out..
    i will let you know what happens..and i would also like to know about the PropertyChangeListener, i have never heard about it,
    i hope you let me know when your power comes back,
    thanks

  5. #5
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,256
    Rep Power
    19

    Default Re: Long Press Button

    Quote Originally Posted by Ksammie01 View Post
    i would also like to know about the PropertyChangeListener, i have never heard about it,
    i hope you let me know when your power comes back,
    thanks
    Don't be so helpless. A search on "PropertyChangeListener Java Tutorial" would have yielded this top result: How to Write a Property Change Listener (The Java™ Tutorials > Creating a GUI With JFC/Swing > Writing Event Listeners)

    The point of using a PCL rather than a MouseListener is that a button press may come from a keyboard action (spacebar in most LaFs).

    db

    db
    Ksammie01 likes this.
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Long Press Button

    Myself, I wouldn't use a PropertyChangeListener but rather would use a ChangeListener added to the JButton's model, and then check the model's isPressed() method inside of the listener and act accordingly.
    DarrylBurke likes this.

  7. #7
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,256
    Rep Power
    19

    Default Re: Long Press Button

    Yup, that's a better approach.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  8. #8
    Ksammie01 is offline Member
    Join Date
    Apr 2012
    Posts
    8
    Rep Power
    0

    Default Re: Long Press Button

    problem solved
    since i was using the mouse to press the button, i used mouse listener..
    got the job done!

  9. #9
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,256
    Rep Power
    19

    Default Re: Long Press Button

    Fubarable's solution is much better; it gives you consistent behavior regardless of how the button is pressed.

    db
    Fubarable likes this.
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  10. #10
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Long Press Button

    Quote Originally Posted by Ksammie01 View Post
    problem solved
    since i was using the mouse to press the button, i used mouse listener..
    got the job done!
    This is a bad solution, very bad as you loose much functionality from the button. For instance, if you disable the JButton, you will prevent the MouseListener from firing. Please don't do this.

Similar Threads

  1. Handle button press
    By TomWaits in forum AWT / Swing
    Replies: 3
    Last Post: 02-22-2011, 09:22 AM
  2. open another window via button press
    By vkokaram in forum New To Java
    Replies: 6
    Last Post: 07-19-2010, 02:09 AM
  3. Replies: 10
    Last Post: 06-06-2010, 06:49 PM
  4. Replies: 0
    Last Post: 05-20-2010, 08:18 PM
  5. simulate form type=button press with java application
    By redmoonzer01 in forum Networking
    Replies: 1
    Last Post: 03-29-2008, 06:28 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •