0

How can I determine if a certain JButton has been pressed in java? I have 2 buttons and depending on which button is pressed a different a value could be true or false.

boolean value = true;

if (event.getSource() == a)
  {
    value == true;
  }
else if (event.getSource() == b)
  { 
    value == false;
  }

right now no matter which button is pressed the value is false

mKorbel
  • 109,525
  • 20
  • 134
  • 319
user2901278
  • 93
  • 1
  • 3
  • 7
  • 5
    `value == true;` is comparison, not assignment – Aurand Dec 05 '13 at 01:00
  • Try printing the name of the button that you get from `event.getSource()` (after setting the name of each button if you haven't already; see http://stackoverflow.com/a/14310353/423105). – LarsH Dec 05 '13 at 03:07
  • 1
    Also, show us the code that populates `a` and `b`, so we can see if you have the right objects in there. – LarsH Dec 05 '13 at 03:08

1 Answers1

2

Looks like you have a typo:

value == true;
value == false;

should be:

value = true;
value = false;

"==" is testing for equality, "=" is for assigning a value to a variable.

camickr
  • 321,443
  • 19
  • 166
  • 288