-1

is there a way to check if the button we get from clicking (mouseClicked) is the same as a button that exists in a array of buttons? I've used both (==) and equals() but neither works.

i'm new to java, please take that in mind.

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
frog
  • 31
  • 6
  • 2
    Consider providing a [runnable example](https://stackoverflow.com/help/mcve) which demonstrates your problem. This is not a code dump, but an example of what you are doing which highlights the problem you are having. This will result in less confusion and better responses – MadProgrammer Jan 07 '16 at 00:36
  • `I've used both (==) and equals() but neither works.` - then I would say your code is wrong because both should work. Post a demo that demonstrates the problem. – camickr Jan 07 '16 at 00:36
  • 1
    You should be using a `ActionListener` when dealing with buttons, not a `MouseListener`, but having said that, it's likely that what you think you're referencing the (button array) and what's on the screen aren't the same thing – MadProgrammer Jan 07 '16 at 00:36
  • but why is this happening? – frog Jan 07 '16 at 01:05
  • Because you have some problem(s) in your code. We can't tell you more without seeing it. – Pshemo Jan 07 '16 at 01:12
  • Isn't a jButton merely an object, which if you compare directly would only result in the comparing of the where the 2 memory addresses are saved as? – hs2345 Jan 07 '16 at 01:21
  • i used ... if(b1.getIcon().toString().equals(b2[i][j].getIcon().toString()) int the loop and with some prints i found out that there is indeed a button with same value as the one i'm looking for... but for some reason it says it's not found – frog Jan 07 '16 at 01:30
  • You question asks about "clicking buttons" but you code is comparing the toString() representation of the Icons of the button. Why? If you want to compare Icons then compare Icons. If you want to compare buttons then compare buttons. Why are you using toString()? Why are you using b1? We have no idea what that variable represents. You have been asked several times to post your "simple code" demonstrating the problem. I don't see the code... – camickr Jan 07 '16 at 01:48
  • This [example](http://stackoverflow.com/a/7706684/230513) shows how to use both `==` and `equals()` for buttons. If you still have problems, please edit your question to include a [mcve] that shows your revised approach. – trashgod Jan 07 '16 at 09:23

2 Answers2

0

== and equals() have different functions:

==

== becomes true if the two references of objects point to the same object, like this:

Object a = new Object();
Object b = a;
System.out.println(a == b);
// prints true because a referres to the same onject as b

equals()

equals() returns true if the objects are equal, meaning not necessarily the same object. It instead checks whether all fields/properties are equal. It's implementation depends on the class.

Felix D.
  • 786
  • 1
  • 11
  • 17
0

One should use ActionListener not MouseListener to handle button clicks. When you do it you may access source of the click this way:

ActionListener al = e -> {
     JButton button = (JButton) e.getSource();
     //search your array here
};
button.addActionListener(al);

Then just go through your array and compare references by == operator. Also I suggest to use a Collection instead of an array and use Collection.contains(T t) method.

Yoda
  • 17,363
  • 67
  • 204
  • 344
  • what is the difference between these two? – frog Jan 07 '16 at 01:09
  • @frog http://stackoverflow.com/questions/13526555/pros-and-cons-of-using-actionlistener-vs-mouselistener-for-capturing-clicks-on – Yoda Jan 07 '16 at 01:15