Home > Archive > Java Help > February 2005 > Get array index on MouseEvent
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Get array index on MouseEvent
|
|
|
| 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.
| |
| klynn47@comcast.net 2005-02-24, 4:01 pm |
| One suggestion I would have is to make a simple extension of JLabel
called MyJLabel. Add one field to it that's an int.
When you create the MyJLabel, send all of the usual information to the
JLabel class through the constructor of MyJLabel and set the value of
the int to the index of the MyJLabel in the array.
Then when you cast the source to a MyJLabel, just retrieve the value of
the int.
| |
| Ulf Nordlund 2005-02-24, 4:01 pm |
| Jay skrev:
> 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.
>
>
>
If you had your labels in an ordered collection (such as an ArrayList),
you should be able to get the index like so:
mylabelCollection.getIndx(evt.getSource());
Another way would be to map label vs. index separately (e.g. HashMap?).
(Or do it like klynn47 suggests. Propably best OO..)
/ulf
| |
|
| Thanks klynn and Ulf. So, does that mean that, in general, an object isn't
able to access info about the array in which it is contained?
Jay
| |
| Tilman Bohn 2005-02-24, 8:59 pm |
| In message <421e1c73$0$22517$2c56edd9@news.cablerocket.com>,
Jay wrote on Thu, 24 Feb 2005 10:20:16 -0800:
> Thanks klynn and Ulf. So, does that mean that, in general, an object isn't
> able to access info about the array in which it is contained?
An object is never contained in an array (nor in any other object),
only a reference to it is. And since an object doesn't have any notion
of what references might be pointing to itself (unless specifically
coded and used that way), it follows that the answer to your question
is yes, that's what it means. (Whatever `it' was, which is hard to
say since you didn't quote any context.)
--
Cheers, Tilman
`Boy, life takes a long time to live...' -- Steven Wright
| |
|
| "Tilman Bohn" <myfirstname@gmx.net> wrote in message
news:slrnd1schs.q8b.myfirstname@urizen.tilmanbohn.com...
> In message <421e1c73$0$22517$2c56edd9@news.cablerocket.com>,
> Jay wrote on Thu, 24 Feb 2005 10:20:16 -0800:
>
>
> An object is never contained in an array (nor in any other object),
> only a reference to it is.
Of course, you're right. I should've used more careful language to prevent
responses such as yours.
> And since an object doesn't have any notion
> of what references might be pointing to itself (unless specifically
> coded and used that way), it follows that the answer to your question
> is yes, that's what it means.
Thanks, that answers my question.
> (Whatever `it' was, which is hard to
> say since you didn't quote any context.)
Thought it was pretty apparent that, since I addressed both respondents, I
was responding to the points made in both their posts... but no matter; you
answered my question. Thanks.
Jay
| |
| Tilman Bohn 2005-02-25, 4:00 am |
| In message <421e5d2f$0$22518$2c56edd9@news.cablerocket.com>,
Jay wrote on Thu, 24 Feb 2005 14:56:22 -0800:
> "Tilman Bohn" <myfirstname@gmx.net> wrote in message
> news:slrnd1schs.q8b.myfirstname@urizen.tilmanbohn.com...
[...]
>
> Thought it was pretty apparent that, since I addressed both respondents, I
> was responding to the points made in both their posts...
Yes, but not everyone has the time to follow every thread very
closely, or the incliniation to look up preceding posts in a thread
just to get context. So by not quoting anything, you artificially
limit the group of people who might usefully follow up to it.
> but no matter; you answered my question. Thanks.
You're very welcome.
--
Cheers, Tilman
`Boy, life takes a long time to live...' -- Steven Wright
| |
|
| "Tilman Bohn" <myfirstname@gmx.net> wrote in message
news:slrnd1tguj.q8b.myfirstname@urizen.tilmanbohn.com...
> In message <421e5d2f$0$22518$2c56edd9@news.cablerocket.com>,
>
> Yes, but not everyone has the time to follow every thread very
> closely, or the incliniation to look up preceding posts in a thread
> just to get context. So by not quoting anything, you artificially
> limit the group of people who might usefully follow up to it.
I would respectfully suggest then that if one doesn't have the time to read
a (short) thread, one should not post a response and then criticize another
poster because one doesn't understand the context.
Jay
| |
| Thomas G. Marshall 2005-02-26, 3:59 pm |
| klynn47@comcast.net coughed up:
> One suggestion I would have is to make a simple extension of JLabel
> called MyJLabel. Add one field to it that's an int.
>
> When you create the MyJLabel, send all of the usual information to the
> JLabel class through the constructor of MyJLabel and set the value of
> the int to the index of the MyJLabel in the array.
>
> Then when you cast the source to a MyJLabel, just retrieve the value
> of the int.
IMHO, this is not particularly clean.
That would mean having the object itself be cognizant of its own order in a
wrapping list somewhere. What if the object was added to multiple lists, or
if one of the JLabel's is removed from the list? Seems a little like
inverted incapsulation to me.
If you have a ArrayList of JLabels, then use the getIndex() method on the
list itself.
IOW, have the container be in charge of what it regards as the index, and
not the object itself.
and *to the OP*, why do you find it necessary to know the index anyway? If
you have the JLabel itself, isn't that all you need? Are you trying to
maintain parallel arrays?
--
"It's easier to be terrified by an enemy you admire."
-Thufir Hawat, Mentat and Master of Assassins to House Atreides
| |
| Thomas G. Marshall 2005-02-26, 3:59 pm |
| Thomas G. Marshall coughed up:
> klynn47@comcast.net coughed up:
>
> IMHO, this is not particularly clean.
>
> That would mean having the object itself be cognizant of its own
> order in a wrapping list
....or array, of course...
> somewhere. What if the object was added to
> multiple lists, or if one of the JLabel's is removed from the list? Seems
> a little like inverted incapsulation to me.
>
> If you have a ArrayList of JLabels, then use the getIndex() method on
> the list itself.
>
> IOW, have the container be in charge of what it regards as the index,
> and not the object itself.
>
> and *to the OP*, why do you find it necessary to know the index
> anyway? If you have the JLabel itself, isn't that all you need? Are
> you trying to maintain parallel arrays?
--
"It's easier to be terrified by an enemy you admire."
-Thufir Hawat, Mentat and Master of Assassins to House Atreides
|
|
|
|
|