0

I was wondering if there was a method for JButtons that is, and I'm just guessing here, essentially a boolean that would be assigned a true or false value depending on whether or not a button is clicked. I understand that there are actionListeners and keyListeners and MouseListeners and a plethora of listeners, but I am searching for a method that would do something like this:

public boolean ButtonClickDetector (just pretend it's real)

{

if(JButton.isClicked())

{

return true;

}

else

{

return false;

}

I need this so that I can increase an integer only when a button is clicked. I have thought about just putting integer++; into the actionPerformed but I feel if I had a boolean, the code would function better, be less prone to error, and perhaps a bit more efficient. Please note, I am a high schooler and do not have oodles and oodles of coding experience so PLEASE dumb down your answers. When answering PLEASE PROVIDE AN EXAMPLE AND EXPLANTION OF WHAT YOU ARE SHOWING ME. Accompanying an example and explantion with documentation would be wonderful. Thanks.

Ungeheuer
  • 1,393
  • 3
  • 17
  • 32
  • 1
    Why not use an action listener for this? – Daniel Kaplan Apr 30 '14 at 21:50
  • No need to reinvent a wheel. Your idea of using `ActionListener` is absolutely correct. – PM 77-1 Apr 30 '14 at 21:52
  • I needed a method that checked if the button was pressed. I guess I wasnt clear enough. I kinda started daydreaming about what would work and got carried away. Props go to Vince Emigh for the correct answer. – Ungeheuer May 01 '14 at 03:05

1 Answers1

2

No, there are no methods in the JButton class that allows you to perform such a check. You must use some kind of listener. Check the JavaDoc for JButton to see what methods are available.

http://docs.oracle.com/javase/7/docs/api/javax/swing/JButton.html

You can use e.getSource() to see where the event came from.

JButton button = new JButton();
int i = 0;

public void actionPerformed(ActionEvent e) {
     if(e.getSource == button) {
          i++;
     }
}

This will allow you to only increase if that specific button was pressed.

Vince
  • 14,470
  • 7
  • 39
  • 84
  • I'm not sure it qualifies as an *answer*. Seems more like a comment to me. – PM 77-1 Apr 30 '14 at 21:54
  • 1
    @PM77-1 is that any better? – Vince Apr 30 '14 at 21:57
  • 1
    there are no methods in the JButton class that allows you to perform such a check. - [ButtonModel can do that](http://stackoverflow.com/a/5755124/714968), and JButtons API has isPressed too, but an answer should be correct ... add ActionListener to JButton – mKorbel Apr 30 '14 at 22:54
  • Yes, it looks like an answer now. – PM 77-1 May 01 '14 at 01:13
  • @mKorbel - What do you mean by "*JButtons API has isPressed too*"? Please clarify. – PM 77-1 May 01 '14 at 01:29
  • Well, .getSource should work but I will hunt down isPressed. I really only needed a method that told me if a button was clicked or not. I imagined that such a method would essentially be a boolean returning true if the was clicked and false if the button was not clicked. So thanks to everyone who answered. – Ungeheuer May 01 '14 at 03:07
  • @mKorbel Thank you as well both .getSource() and isPressed() seem to work quite well. Thank you. As a side question, since these two methods seem to be interchangable, is there a specific instance in which I SHOULD choose one over the other, or is it just pick whichever one I pick? – Ungeheuer May 01 '14 at 03:09
  • @VinceEmigh for some reason this isnt working. TO verify functionality I put a System.out.println(i); into the if-statement, but it doesnt print. It just skips to the next action. – Ungeheuer May 01 '14 at 22:48
  • @user3519829 print out `e.getSource()` and see what you get. Youre gonna have to post your code if you want me to analyze, but `e.getSource()` returns the component that triggered the event – Vince May 01 '14 at 23:31
  • @VinceEmigh This is what System.out.println(e.getSource()); printed:`javax.swing.JButton[,0,334,1024x167,alignmentX=0.0,alignmentY=0.5,border=javax.swing.plaf.BorderUIResource$CompoundBorderUIResource@188ce6aa,flags=296,maximumSize=,minimumSize=,preferredSize=,defaultIcon=,disabledIcon=,disabledSelectedIcon=,margin=javax.swing.plaf.InsetsUIResource[top=2,left=14,bottom=2,right=14],paintBorder=true,paintFocus=true,pressedIcon=,rolloverEnabled=true,rolloverIcon=,rolloverSelectedIcon=,selectedIcon=,text=Hit Me,defaultCapable=true]` – Ungeheuer May 02 '14 at 00:53
  • There was no error or anything, but this is what came out. Is it supposed to print this, or just the name of the Button? For the code above, the name of the JButton is "HitMe" and the text printed on the JButton is "Hit Me" – Ungeheuer May 02 '14 at 00:53
  • @user3519829 I was making sure everything was working (your button is firing events). Edit your question and post what your class looks like. Im not 100% sure, but it could be a reference problem (when using ==, you're checking for reference. If you passed the button through some kind of parameter before getting to `e.getSource() == button`, that could be the problem, since java is pass-by-value and not by reference). You must refer to the same reference. You cannot pass your button to a new reference after adding the listener, and use the new reference for the statement – Vince May 02 '14 at 01:11
  • @VinceEmigh I will post a new question and put a comment on this one with the link. – Ungeheuer May 02 '14 at 02:28
  • @user3519829 There's no need for a new question. You can edit this question, and make it suitable for future reference. The point of StackOverflow is to ask questions that haven't been asked before. That way, it can be a page of information that others can reference to later. Creating a new question would be seen as spam (probably get marked as duplicate). If you edit your question, I will edit my answer – Vince May 02 '14 at 02:55
  • 1
    @VinceEmigh okey doke – Ungeheuer May 02 '14 at 21:38