I have MouseListener with MouseListener
label.addMouseListener( new ClickController() );
where are the actions performed in
class ClickController{ ... public void mouseClicked(MouseEvent me) {
Is there a way to associate an object with JLabel so that I can access it from the mouseClicked method?
Edit:
To give a better example, what I'm trying to do here is set JLabels as a graphical representation of playing cards. The label is intended to represent an object map containing all the actual data. Therefore, I want to associate this map object with JLabel.
Decision:
As Hovercraft Full Of Eels says, me.getSource() is the way to go. In my particular case, it would be:
Card card = new Card(); label.putClientProperty("anythingiwant", card); label.addMouseListener( new ClickController() );
and get the Card object from the listener:
public void mouseClicked(MouseEvent me) { JLabel label = (JLabel) me.getSource(); Card card = (Card) label.getClientProperty("anythingiwant");
source share