If you have two JLabels in a JFrame both with the same MouseListener click event added to them, how can you tell which JLabel was clicked without creating a second actionlistener?
Note: both labels have the same text written on them so that cannot be used to tell them apart.
This will get you a reference to the component ...
public void mousePressed(MouseEvent e)
{
JComponent reference = e.getComponent();
}
For a more complete description look at the Swing Tutorial on MouseListeners
Just make the two JLabels fields and then check the source of the MouseEvent:
if (e.getSource() == firstLabel) {
...
} else if (e.getSource() == secondLabel) {
...
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With