Search code examples
javaswingjpanelactionlistener

compiler dont recognize parameters in action listener class


im trying to build matrix of JPanels and button of clear, when you press the screen the panel in the specific place colored in black , and when you press clear everything got white. i dont know why but in my action listener class it dont recognize paramrters and object from the Painters class.

Cell.java

package Paint;

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

public class Cell extends JPanel {

    private JPanel p;

    public Cell() {
        this.p = new JPanel();
        this.setSize(50, 50);
        this.setBackground(Color.white);
    }
}

Matrix.java

package Paint;

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

public class Matrix extends JPanel {

    private Cell[][] matrix;

    public Matrix(int row , int col) {
        this.setLayout(new GridLayout(row, col));
        this.matrix = new Cell[row][col];
        for(int i=0; i< row ; i++) {
            for(int j=0; j<col; j++) {
                this.matrix[i][j] = new Cell();
            }
        }
    }
}

Painter.java

package Paint;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;    
import javax.swing.*;

public class Painter extends JPanel {

    private JButton btn;
    private Matrix matrix ;
    private int row , col;

    public Painter(int row , int col) {
        this.row = row;
        this.col = col;
        this.matrix = new Matrix(row, col);
        this.setLayout(new BorderLayout());
        btn = new JButton("clear");
        this.add(matrix , BorderLayout.CENTER);
        this.add(btn , BorderLayout.SOUTH);
    }

    private class MyActionListener implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            if(e.getSource() == btn) {
                for(int i= 0; i< row; i++) {
                    for (int j= 0 ; j < col ; j++) {
                        this.matrix[i][j].setBackground(Color.white);
                    }
                }
            } else {
                for(int i= 0; i< this.row; i++) {
                    for (int j= 0 ;this.col ; j++) {
                        if(e.getSource()== this.matrix[i][j]) {
                            if(this.matrix[i][j].getBackground()== Color.white)
                                this.matrix[i][j].setBackground(Color.black);
                            else
                                this.matrix[i][j].setBackground(Color.white);
                        }
                    }
                }
            }
        }
    }
}

Tester.java

package Paint;

import javax.swing.JFrame;    
import ShayExam.MyPanel;    
import javax.swing.JFrame;

public class Tester {

    public static void main(String[] args) {
        JFrame frame = new JFrame("matrix");
        frame.setSize(750, 750);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        System.out.println("before Painter");
        Painter p = new Painter( 6 , 6);
        frame.add(p);
        frame.setVisible(true);
    }
}

Solution

  • Try doing this, "this" refers to the calling instance of the class in which you are using "this" keyword. You can access data member of outer class directly in inner class. You can't use col in conditional part of for loop. actionPerfomed() will not get called because you have to add ActionListener to any instance.

    This code does not have compilation error.

    private class MyActionListener implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            if(e.getSource() == btn) {
                for(int i= 0; i< row; i++) {
                    for (int j= 0 ; j < col ; j++) {
                        matrix.getCell(i,j).setBackground(Color.white);
                    }
                }
            } else {
                for(int i= 0; i< row; i++) {
                    for (int j= 0 ;j<col ; j++) {
                        if(e.getSource()== matrix.getCell(i,j)) {
                            if(matrix.getCell(i,j).getBackground()== Color.white)
                                matrix.getCell(i,j).setBackground(Color.black);
                            else
                                matrix.getCell(i,j).setBackground(Color.white);
                        }
                    }
                }
            }
        }
    }
    

    and Matrix class

    public class Matrix extends JPanel {
    
        private Cell[][] matrix;
    
        public Matrix(int row , int col) {
            this.setLayout(new GridLayout(row, col));
            this.matrix = new Cell[row][col];
            for(int i=0; i< row ; i++) {
                for(int j=0; j<col; j++) {
                    this.matrix[i][j] = new Cell();
                }
            }
        }
    
        public Cell getCell(int i, int j) {
            return matrix[i][j];
        }
    }