Determine Object that Invokes an Event Handler

I am writing a program in Java that has an array of JButtons and they all need to use the same event handler. The problem is that the event handler needs to make changes to each button. Because of this, I need to be able to determine the object that called the event handler and make changes to it. I have messed around with this for a while. I searched on Google for java get name of object calling event handler but did not find anything helpful.

Here's a copy of what I have so far with all of the extra program code stripped out.

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.Arrays;
import java.util.Scanner;
import java.util.Random;
import java.*;

public class MyJavaProgram extends JFrame implements ActionListener
{
    // Buttons
    JButton[] buttonsArray = new JButton[20];

    public MyJavaProgram()
    {   
        // Fonts
        Font arial = new Font("Arial", Font.PLAIN, 25);

        for(int x = 0; x < buttonsArray.length; x++)
        {
            buttonsArray[x] = new JButton(Integer.toString(x + 1));
            buttonsArray[x].setFont(arial);
            buttonsArray[x].addActionListener(this);
        }

        // Get the content pane and set the layout.
        Container jPane = getContentPane();
        jPane.setLayout(new GridLayout(8, 10)); // (rows, columns)

        // JFrame general settings.
        setTitle("My Java Program");
        setSize(700, 500); // width, height
        setVisible(true);
        setResizable(false);
        setDefaultCloseOperation(EXIT_ON_CLOSE); // Without this, the program will continue running even if the X is clicked.

        // Add our stuff to the JFrame.
        for(int x = 0; x < buttonsArray.length; x++)
            jPane.add(buttonsArray[x]);
    }

    public void actionPerformed(ActionEvent e)
    {
        System.out.println("Event triggered by one of the 20 buttons.");
    }

    public static void main(String[] args)
    {
        MyJavaProgram programUI = new MyJavaProgram();
    }
}
Jon Skeet
people
quotationmark

That's precisely what the source of the ActionEvent is for:

public void actionPerformed(ActionEvent e)
{
    JButton button = (JButton) e.getSource();
    System.out.println("Event triggered by " + button.getText());
}

people

See more on this question at Stackoverflow