I\'m trying to build a program that utilizes a 3x3 grid of buttons (using Java Swing), so I initialize it with a GridLayout and a loop to create the buttons:
you already keep a track of the buttons by the array index i.e. buttray[i]. Use getSource()
public void actionPerformed(ActionEvent e)
{
// TODO Auto-generated method stub
for(int i=0;i<buttray.length;i++)
if(e.getSource()==buttray[i])
{
//code here
}
}
There are multiple ways to distinguish which button fired the ActionEvent:
if (e.getActionCommand().equals("Top Left")
)if (e.getSource() == buttray[0] )
)if (e.getSource().getText().equals("Top Left")
)if (e.getSource().getName().equals("Top Left")
)