Skip to main content

get objet reference on mouse event

Please note these java.net forums are being decommissioned and use the new and improved forums at https://community.oracle.com/community/java.
1 reply [Last post]
spimees
Offline
Joined: 2011-05-17

hi everybody!

i am new in java and i made this class below.
i was wondering if i can get a reference of the clicked item so i can call my SelectItem(Item item) function.

public class Item extends JPanel implements MouseListener {
/**
*
*/
private static final long serialVersionUID = 1L;

private int itemValue;
private int itemProb;
private int luminance;
private boolean intrash= false;

//ctor
public Item(int v, int p){

this.setItemValue(v);
this.setItemProb(p);
//mouse listner
addMouseListener (this);
this.setBorder(new EtchedBorder());
}

public void mouseClicked (MouseEvent e) {

//i want to call this function
SelectItem(Item item);
}
.
.
.

}

i instantiate item objects from another class an add them in a mainPanel

what i should do to get an item object reference when the user click on the item, i want this cause a have a vector of Item objects and i want to know which one of them was selected so i do some things and change his color

thank you, tm

here's a screenshot

AttachmentSize
Untitled.png67.76 KB

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
kschaefe
Offline
Joined: 2006-06-08

You need to ask Swing questions in the Swing forum; this forum is for SwingLabs and SwingX.

Karl