public void actionPerformed(ActionEvent ae) {
// Who is sending this event?
System.out.println("source class name = " +
ae.getSource().getClass().getName());
// If JButton is the only kind/type of component
// that has registered with this ActionListener
// then this cast will be safe.
JButton a = (JButton)ae.getSource();
// Get the text on the button.
String whichButton = a.getText();
// Or, you can do:
String ac = ae.getActionCommand();
// Test for a certain button whose text is "right"
if(ac.equals("right"))
; // found it
System.out.println("whichButton = " + whichButton);
test.setText(whichButton);
changeLights(whichButton);
}
For another option you can use the
setActionCommand(String) method for each button to set the actionCommand you want to use. This is especially useful if your app will be used in an international setting: the button text may be translated into another language and may/wll not match the string you test for in your event code. For example, the "right" button in Spanish may have the text translated to "dereche."