Search code examples
javaswingnetbeansjbutton

Creating events for a buttons programmatically in Java


I have an array of jbuttons. I wanted to add Home_MouseDown procedure, to the mousePressed event of the jbutton. Below is the array of JButton that I created.

Code:

    Jbutton[][] home = new JButton[8][8];

    for (int i = 0; i < 8; i++) {
        for (int j = 0; j < 8; j++) {                
            home[j][i] = new JButton();

            this.add(home[j][i]);
        }
    }

Solution

  • Home[j][i].addActionListener(new ActionListener(){
    
       @Override
        ActionPerformed(ActionEvent evt){
       //w.e function u wanna add here on mouse click
        }
        });