Results 1 to 20 of 20
  1. #1
    jiffi is offline Member
    Join Date
    Oct 2011
    Posts
    79
    Rep Power
    0

    Default Differentiating user clicks and doClick clicks?

    Just this simple question, I was wondering if there was a way to differentiate user generated clicks (on a JButton) from a computer generated click (using the doClick() method). Thanks

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default Re: Differentiating user clicks and doClick clicks?

    Override the doClick method and catch it there.

  3. #3
    jiffi is offline Member
    Join Date
    Oct 2011
    Posts
    79
    Rep Power
    0

    Default Re: Differentiating user clicks and doClick clicks?

    can you please explain in more details?

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default Re: Differentiating user clicks and doClick clicks?

    If you override a class's method, then if that method is called, your overriding code will get control.
    Extend the JButton class, override the method and add that new class to the GUI. Any calls to the method will be to your code.

  5. #5
    jiffi is offline Member
    Join Date
    Oct 2011
    Posts
    79
    Rep Power
    0

    Default Re: Differentiating user clicks and doClick clicks?

    ok thanks, but I don't think that will help.
    I need to know when the user clicks and when the computer clicks on the same button, because sometimes the user clicks need to be ignored.

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default Re: Differentiating user clicks and doClick clicks?

    How does the computer do the click?

  7. #7
    jiffi is offline Member
    Join Date
    Oct 2011
    Posts
    79
    Rep Power
    0

    Default Re: Differentiating user clicks and doClick clicks?

    Using JButton.doClick() when it is the computer's turn to "play"

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default Re: Differentiating user clicks and doClick clicks?

    If you override the method then you will know that it was called by the computer.
    If it was not called, then the user clicked.

  9. #9
    jiffi is offline Member
    Join Date
    Oct 2011
    Posts
    79
    Rep Power
    0

    Default Re: Differentiating user clicks and doClick clicks?

    I understand what you are saying but I don't know how to code that.

  10. #10
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default Re: Differentiating user clicks and doClick clicks?

    What part don't you know how to do?
    How to extend a class?
    How to override a method?

  11. #11
    jiffi is offline Member
    Join Date
    Oct 2011
    Posts
    79
    Rep Power
    0

    Default Re: Differentiating user clicks and doClick clicks?

    im not so sure about neither of those. I'm guessing overriding is done using @Override.

  12. #12
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default Re: Differentiating user clicks and doClick clicks?


  13. #13
    jiffi is offline Member
    Join Date
    Oct 2011
    Posts
    79
    Rep Power
    0

    Default Re: Differentiating user clicks and doClick clicks?

    if it is just extends classname then I do know how to extend.
    I just got even more confused then before..

  14. #14
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default Re: Differentiating user clicks and doClick clicks?

    Do you know how to override a method of the class that has been extended?

  15. #15
    jiffi is offline Member
    Join Date
    Oct 2011
    Posts
    79
    Rep Power
    0

    Default Re: Differentiating user clicks and doClick clicks?

    that on the other hand is a no, but i'm trying to understand how it works with your link

    from your link, the overrinding is happening here?

    Animal myAnimal = myCat;
    myAnimal.testInstanceMethod();
    Last edited by jiffi; 12-26-2011 at 02:05 AM.

  16. #16
    jiffi is offline Member
    Join Date
    Oct 2011
    Posts
    79
    Rep Power
    0

    Default Re: Differentiating user clicks and doClick clicks?

    Ok so I can't make it work, I tried writing public void doClick(), make a new class inside GUI with public void doClick() making one outside etc and none of them seem to work. Please help!!

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

    Default Re: Differentiating user clicks and doClick clicks?

    You override by extending a class and override the methods whose behaviors you want to change. For example:

    Java Code:
    class MyButton extends JButton {
       public MyButton(String title) {
          super(title);
       }
    
       // plus any other constructors that you want to imitate that of JButton...
       
       @Override
       public void doClick() {
          System.out.println("software do click!");
          super.doClick();
       }
    }
    But this is going through a lot of trouble for limited benefit. Why do you even want to do this? And realize doing this will not alter the behavior of any ActionListeners added to the JButton. Your original question has a bad code smell to it.

  18. #18
    jiffi is offline Member
    Join Date
    Oct 2011
    Posts
    79
    Rep Power
    0

    Default Re: Differentiating user clicks and doClick clicks?

    Thanks for your answer.

    There are some things that I want to happen when the computer clicks that don't when the user clicks. I will try out your solution ASAP.

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

    Default Re: Differentiating user clicks and doClick clicks?

    Quote Originally Posted by jiffi View Post
    Thanks for your answer.

    There are some things that I want to happen when the computer clicks that don't when the user clicks.
    Yep, this has a bad smell. The computer shouldn't "click" at all but should call methods. Thus your question really is a non-issue since the correct answer is not to have your program call doClick().

  20. #20
    jiffi is offline Member
    Join Date
    Oct 2011
    Posts
    79
    Rep Power
    0

    Default Re: Differentiating user clicks and doClick clicks?

    I still can't make it work. Where in my code should I place this?

Similar Threads

  1. How do I check if user clicks on an image?
    By rajkobie in forum New To Java
    Replies: 10
    Last Post: 05-04-2011, 04:45 AM
  2. Responding to clicks outside a window
    By bayan in forum New To Java
    Replies: 6
    Last Post: 06-14-2010, 04:51 PM
  3. The trouble with handling the button clicks
    By Borneq in forum New To Java
    Replies: 3
    Last Post: 01-10-2010, 02:57 PM
  4. How to swap to images on clicks
    By ntagrafix in forum AWT / Swing
    Replies: 2
    Last Post: 12-03-2009, 08:13 PM
  5. [SOLVED] Intercepting mouse clicks
    By carderne in forum New To Java
    Replies: 6
    Last Post: 02-28-2009, 07:10 PM

Posting Permissions

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