0

I'm trying to print which JPanel in JPanel array has been clicked using mouseEvent. How do I do that?

It gives me an error:

Local variable i defined in an enclosing scope must be final or effectively final

for(int i=0; i<count[0]; i++) {
    p1[i] = new JPanel();
    l1[lcount] = new JLabel("Panel "+(i+1));
    p1[i].add(l1[lcount]);
    panel_2.add(p1[i]);
    lcount++;
    p1[i].addMouseListener(new MouseAdapter() {
        @Override
        public void mouseClicked(MouseEvent e) {
            System.out.println(i);
        }
    });
}

I want to extract the value of i and display it in another JLabel.

Frakcool
  • 10,915
  • 9
  • 50
  • 89
valar_007
  • 13
  • 2
  • Seems like a duplicate from [this question](https://stackoverflow.com/questions/55941775/how-to-make-jbutton-know-on-which-panel-it-has-been-clicked-on/55941849#55941849) and the answer is the same. And if you did a [Google search](https://www.google.com/search?q=java+variable+must+be+final&oq=java+variable+must+be+&aqs=chrome.1.69i57j0l5.10352j0j7&sourceid=chrome&ie=UTF-8) for the error you get you'll get to [this question and answer](https://stackoverflow.com/a/34866174/2180785) which explicitly says why you get that error. – Frakcool May 02 '19 at 17:11
  • 1
    Possible duplicate of [Pass variables to ActionListener in Java](https://stackoverflow.com/questions/11037622/pass-variables-to-actionlistener-in-java) – Frakcool May 02 '19 at 17:14
  • Also possible duplicate of https://stackoverflow.com/questions/39483665/error-local-variable-referenced-from-inner-class-must-be-final-or-effective-fin – Frakcool May 02 '19 at 17:15

1 Answers1

0

You can use the e.getSource() from you mouseEvent(). Just cast it to JPanel.

Here is an example.

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class MouseEventSource {
   JFrame frame = new JFrame();

   public static void main(String[] args) {
      new MouseEventSource().start();
   }

   public void start() {
      JPanel p1 = createPanel("Panel 1", Color.BLUE);
      JPanel p2 = createPanel("Panel 2", Color.RED);
      MyMouseListener ml = new MyMouseListener();
      p1.addMouseListener(ml);
      p2.addMouseListener(ml);
      frame.setLayout(new FlowLayout());
      frame.add(p1);
      frame.add(p2);
      frame.pack();
      frame.setLocationRelativeTo(null);
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.setVisible(true);
   }

   public JPanel createPanel(String name, Color color) {
      JPanel panel = new JPanel() {
         public String toString() {
            return name;
         }
      };
      panel.setBackground(color);
      panel.setPreferredSize(new Dimension(250, 250));
      return panel;
   }

   private class MyMouseListener extends MouseAdapter {
      public void mouseClicked(MouseEvent e) {
         // not really necessary to print toString()
         JPanel panel = (JPanel) e.getSource();
         System.out.println(panel);
      }
   }
}
WJS
  • 36,363
  • 4
  • 24
  • 39