Get array index on MouseEvent

From: Jay (jay_at_spamthis.com)
Date: 02/24/05


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.



Relevant Pages

  • Re: Problem updating a label
    ... so your JLabel (along with the rest of your GUI) doesn't ... ActionListener l = new ActionListener{ ... public void actionPerformed{ ...
    (comp.lang.java.programmer)
  • Text to other panels
    ... that is for inventory. ... JLabel Title=new JLabel; ... // Message Panel ... public void init3() ...
    (comp.lang.java.programmer)
  • Problem With Threads
    ... program that would display a jlabel through a thread. ... public void start{ ... Oddly enough, the System.out.println statement works fine, the jlabel ... if I put an infinite loop in ...
    (comp.lang.java.help)
  • Re: using HTML in JLabels
    ... I am currently devloping a java application that has a dialogue box ... with a JLabel with HTML embedded into it the label. ... JEditorPane has a built in listener for catching HyperlinkEvents. ... public void hyperlinkUpdate{ ...
    (comp.lang.java.help)
  • Re: Problem With Threads
    ... > program that would display a jlabel through a thread. ... public void run{ ... if I put an infinite loop in ...
    (comp.lang.java.help)