I’ve made a array full of JLabels and would like to add a listener to them.
The listener doesn’t need to know exactly which one was clicked on, just that one was.
Is there a way to add the listener to the whole array instead of using a ‘for()’ loop ?
Thanks for reading.
If your labels are added a to a container ( like a JPanel ) you can add a listener to this container and know which component is at certain location.
In this case I use a mouseListener because that give me the location where the user clicked.
And prints correctly what component was clicked.
The full source code is here