title: Detect which JLabel is clicked
body: <p>I have a <code>JLabel[10]</code> and I want to detect which label has been clicked and print which label of the label that has been clicked.</p>
  <ol>
  <li>I created a <code>JLabel</code> array of 10.</li>
  <li>Wrote a for loop to place an Image to every position of the label.</li>
  <li>Added a <code>MouseListener</code> to check which label has been clicked.</li>
  </ol>
  <p>The problem is I can't do this to get the source of my <code>jLabelArr</code>. The program will ask me to change my it to final.</p>
  <pre><code>   if(e.getSource() == jLabelArr[i]) 
  </code></pre>
  <h2>Full code</h2>
  <pre><code>import java.awt.FlowLayout;
  import java.awt.event.MouseAdapter;
  import java.awt.event.MouseEvent;
  import javax.swing.ImageIcon;
  import javax.swing.JFrame;
  import javax.swing.JLabel;
  import javax.swing.JPanel;
  
  public class JavaLabels extends JFrame {
  
  private JLabel[] jLabelArr;
  private JPanel jLabelPanel = new JPanel();
  
      public JavaLabels() {
          setLayout(new FlowLayout());
          jLabelArr = new JLabel[10];
          for(int i =0; i &lt; 10; i++) {
  
              jLabelArr[i] = new JLabel(new ImageIcon(&quot;resources/image&quot;));
              jLabelPanel.add(jLabelArr[i]); 
  
              jLabelArr[i].addMouseListener(new MouseAdapter() {
                  @Override
                  public void mouseClicked(MouseEvent e) {
                      if(e.getSource() == jLabelArr[i]) {
                          System.out.println(&quot;Label&quot; + i + &quot;was clicked&quot;);
                      }
                  }
              });
          }
          add(jLabelPanel);
          setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
          pack();
          setSize(400,600);
          setLocationRelativeTo(null);
          setVisible(true);
      }
      public static void main(String[] args) {
          new JavaLabels();
      }
  }
  </code></pre>