Hi all

I've tried to find out which object is behind the JLabel that caused the MouseEvent. I have a code like this.

for(int i = 0; i < annetutKäsikortit.size(); i++) {
	URL _kuva = this.getClass().getResource("/kortit/" + annetutKäsikortit.get(i).haeNumero() + "_pieni.png");
	käsikortitJLabel.add(new JLabel(new ImageIcon(_kuva)));
	käsikortitJLabel.get(i).addMouseListener(hiirenkuuntelija);
	omatKortit.add(käsikortitJLabel.get(i));
}

Sorry about that strange language in the code, let me explain. annetutKäsikortit is an ArrayList which contains the objects given by an another class. _kuva is a temporary variable, which contains the image. haeNumero-method returns a String-object. That all works well. käsikortitJLabel is an ArrayList, where I put those JLabels which I created. omatKortit is a JPanel element, where I draw those images. When I click one of those JLabels, I got my hiirenkuuntelija event listener class make what I want. But I can't find out which Kortti-object is under the JLabel which was just clicked. annetutKäsikortit contains the Kortti-objects and käsikortitJLabel contains the JLabels, they are in the same order. So in principle käsikortitJLabel[0] = annetutKäsikortit[0], but in practise it's not. Kortti-object cannot be compared to a JLabel object.

So what I need? I need to know what is the index of the JLabel in the käsikortitJLabel-ArrayList which was just clicked.

If you didn't understand, I can explain more and answer your questions with pleasure.


Thanks

Mikko

A few possibilities:
You could extend JLabel to maintain a reference to the object, in which case the listener could retrieve it directly from the clicked label.
You could create a listener that keeps a reference to the object and create a new instance for each JLabel.
You could use a Map to store the label to object mappings.

Edited 5 Years Ago by Ezzaral: n/a

Comments
still don't undestand OP's reason +1

A few possibilities:
You could extend JLabel to maintain a reference to the object, in which case the listener could retrieve it directly from the clicked label.
You could create a listener that keeps a reference to the object and create a new instance for each JLabel.
You could use a Map to store the label to object mappings.

How does the fist one way works? How could JLabel maintain a reference to an another object?

No comments for the second way..

Yea, this third one is actually very simply way, I had almost forgotten that there is a Map data structure. I think I'll go with this one, but could you explain me that first one?

and now I need to say that without posting here (your) runnable code is everything in academic ..., and with some important mistakes

You could extend JLabel to maintain a reference to the object, in which case the listener could retrieve it directly from the clicked label...

This would be the obvious strategy, but there's no need to extend JLabel to achieve it.
Every swing component has a "client property" map where you can put and get whatever objects you want relating to that JComponent.

// when creating the JLabels...
theJLabel.put("kortti object" , theRelevantKortti);

// in the event handler
Kortti theRelevantKortti = (Kortti) e.getSource.get("kortti object");

IMHO JComponent client properties are a good candidate for the "most valuable yet totally underused" feature in Swing.

Edited 5 Years Ago by JamesCherrill: n/a

if you use individual event handler for label then it will be easier for you ....

like...

Label1.addMouseListener(new MouseListener(){

                    @Override
                    public void mouseClicked(MouseEvent e) {
                    }

                    @Override
                    public void mousePressed(MouseEvent e) {
                    }

                    @Override
                    public void mouseReleased(MouseEvent e) {
                    }

                    @Override
                    public void mouseEntered(MouseEvent e) {
                    }

                    @Override
                    public void mouseExited(MouseEvent e) {
                    }
                    
                });

Same for every Label where you are facing problem in Identification

Arrrrgghhhh - just woke up and re-read my previous post. Code was garbage. Here is what I should have written

// when creating the JLabels...
theJLabel.putClientProperty("kortti object" , theRelevantKortti);

// in the event handler
Kortti theRelevantKortti = (Kortti) e.getSource.getClientProperty("kortti object");

A thousand apologies...
J

> How does the fist one way works? How could JLabel maintain a reference to an another object?

class LinkedLabel extends JLabel{
    Object linkedObject;
    
    public LinkedLabel(Object linkedObject) {
        this.linkedObject = linkedObject;
    }

    public Object getLinkedObject() {
        return linkedObject;
    }
}

You can then cast e.getSource() to LinkedObject in your listener to retrieve the linked object.

Edited 5 Years Ago by Ezzaral: n/a

How could JLabel maintain a reference to an another object?

Through its client property map (just thought I'd mention it).

Comments
Good suggestion too. I always forget about that.
This article has been dead for over six months. Start a new discussion instead.