Get array index on MouseEvent
From: Jay (jay_at_spamthis.com)
Date: 02/24/05
- Next message: A.Tamboer: "Netbeans"
- Previous message: David Alex Lamb: "comp.lang.java.{help,programmer} - what they're for (mini-FAQ 2004-10-08)"
- Next in thread: klynn47_at_comcast.net: "Re: Get array index on MouseEvent"
- Reply: klynn47_at_comcast.net: "Re: Get array index on MouseEvent"
- Reply: Ulf Nordlund: "Re: Get array index on MouseEvent"
- Reply: Jay: "Re: Get array index on MouseEvent"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Thu, 24 Feb 2005 02:30:04 -0800
Hi,
Is it possible to get the index of an item stored in an array via a
MouseEvent? E.g., if a GUI uses a JLabel array to create a grid of JLabels,
and a MouseListener is used on each JLabel, can the index of a given JLabel
be retrieved when a user clicks on it? The following code will hopefully
clarify what I'm asking:
===================================================
private JLabel[] lblArray = new JLabel[8];
public void addArray() {
for (int i = 0; i < lblArray.length; i++) {
lblArray[i] = new JLabel("" + i);
lblArray[i].addMouseListener(this);
add(lblArray[i]);
}
}
public void mouseClicked(MouseEvent e) {
JLabel tempLabel = (JLabel)e.getSource(); // Creates reference to orig
object... is this necessary to get index?
// Imaginary use of "getIndex()". There're a lot of "getIndex()" methods
in the API, but none seem to apply here... not sure what to do.
JOptionPane.showMessageDialog(null, tempLabel.getIndex());
}
===================================================
Thanks.
- Next message: A.Tamboer: "Netbeans"
- Previous message: David Alex Lamb: "comp.lang.java.{help,programmer} - what they're for (mini-FAQ 2004-10-08)"
- Next in thread: klynn47_at_comcast.net: "Re: Get array index on MouseEvent"
- Reply: klynn47_at_comcast.net: "Re: Get array index on MouseEvent"
- Reply: Ulf Nordlund: "Re: Get array index on MouseEvent"
- Reply: Jay: "Re: Get array index on MouseEvent"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|
|