Button Action Command

Normally you have your applet listen for actions from your controls like the buttons in your applet. The event handler gets the action command. That command is normally the text on the button itself.

Recently I have been creating buttons that have no text. They just display an image that I loaded as the icon of the button. How does the applet know which control generated the event?

It turns out that you can call setActionCommand and pass it a String. This String is when the applet will get when it tries to get the command. It puts you back in control for the event processing.

Here is a tip I discovered the hard way. Make sure you addActionListener(this) on the button you want to handle events for. Otherwise the applet will never get a notification that your button was clicked. This might be obvious. But it is painful to debug when you forget to make the call.