简体   繁体   中英

add mouseListener to an array of JPanels

i have an 2D array with JPanels and i want to add a mouseListener in every JPanel in the array so i use 2 for loops to add them ,but i want to pass the variables i used in for loops in every mouseListener but when i try to do that all mouseListener have the same value of the last variables used in the last for loops .so what am i doing wrong ?

here is my code :

 for (i=0 ; i<3; i++) {
    for (k=0; k<3; k++) {
       a[i][k].addMouseListener(new MouseAdapter() {
            public void mousePressed(MouseEvent e){
                  temp = a[i-1][k];
                  a[i-1][k] = a[i][k];
                  a[i][k] = temp;
                  //some
                  //code here 
            public void mouseClicked (MouseEvent e) {}
            public void mouseReleased(MouseEvent e) 
                {
                    invalidate();
                    revalidate();
                    repaint();
                }
            public void mouseEntered (MouseEvent e) 
                {}
            public void mouseExited  (MouseEvent e) {

                }


            });

        }
    }

I just need to know if there is a way to pass to mouselisteners the variables i,k as arguments to the mouseListener

You can only pass final local variables and class fields into anonymous methods.

I recommend creating a new class that implements MouseAdapter that takes the array and the appropriate indices as arguments in the constructor. Then you can save them as fields in the class and use them when the MouseEvent s are called.

If you need to access additional variables that you haven't mentioned here, you can always pass them into this new class's constructor.

Code:

public AppletMouseListener extends MouseAdapter {
  private final JApplet theApplet;
  private final Container[][] a;
  private final int i;
  private final int j;

  public AppletMouseListener(JApplet theApplet, Container[][] a, int i, int k) {
    this.theApplet = theApplet;
    this.a = a;
    this.i = i;
    this.k = k;
  }

  @Override
  public void mousePressed(MouseEvent e) {
    JComponent temp = a[i-1][k];
    a[i-1][k] = a[i][k];
    a[i][k] = temp;
    //some
    //code here 
  }

  @Override
  public void mouseReleased(MouseEvent e) {
    theApplet.invalidate();
    theApplet.revalidate();
    theApplet.repaint();
  }
}

Your code will be clearer if you use a normal (named) class instead of an anonymous one. Then you can pass the relevant things ( a , i , and k ) to the constructor.

Anonymous classes can't have constructors but they can access local variables declared final .

for (int ii=0 ; ii<3; ii++) {
    for (int kk=0; kk<3; kk++) {
        final int i = ii;
        final int k = kk;
        a[i][k].addMouseListener(new MouseAdapter() {
            public void mousePressed(MouseEvent e){
                JPanel temp = a[i-1][k]; // index out of bounds
                a[i-1][k] = a[i][k];
                a[i][k] = temp;
            }
            public void mouseClicked (MouseEvent e) {}
            public void mouseReleased(MouseEvent e)
            {
                invalidate();
                revalidate();
                repaint();
            }
            public void mouseEntered (MouseEvent e) {}
            public void mouseExited  (MouseEvent e) {}
        });
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM