Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / February 2005

Tip: Looking for answers? Try searching our database.

Get array index on MouseEvent

Thread view: 
Jay - 24 Feb 2005 10:30 GMT
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 - 24 Feb 2005 13:22 GMT
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.
Thomas G. Marshall - 26 Feb 2005 18:47 GMT
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.
[quoted text clipped - 5 lines]
> 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?

Signature

"It's easier to be terrified by an enemy you admire."
-Thufir Hawat, Mentat and Master of Assassins to House Atreides

Thomas G. Marshall - 26 Feb 2005 18:48 GMT
Thomas G. Marshall coughed up:
> klynn47@comcast.net coughed up:
>> One suggestion I would have is to make a simple extension of JLabel
[quoted text clipped - 11 lines]
> 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
[quoted text clipped - 9 lines]
> anyway?  If you have the JLabel itself, isn't that all you need?  Are
> you trying to maintain parallel arrays?

Signature

"It's easier to be terrified by an enemy you admire."
-Thufir Hawat, Mentat and Master of Assassins to House Atreides

Ulf Nordlund - 24 Feb 2005 17:42 GMT
Jay skrev:
> Hi,
>
[quoted text clipped - 25 lines]
>
> 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
Jay - 24 Feb 2005 18:20 GMT
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 - 24 Feb 2005 19:57 GMT
> 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.)

Signature

Cheers, Tilman

`Boy, life takes a long time to live...'      -- Steven Wright

Jay - 24 Feb 2005 22:56 GMT
>> 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.

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 - 25 Feb 2005 06:19 GMT
[...]
>> (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...

 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.

Signature

Cheers, Tilman

`Boy, life takes a long time to live...'      -- Steven Wright

Jay - 26 Feb 2005 00:25 GMT
> 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


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.