View RSS Feed

Java Swing

How to Write an Action Listener

Rate this Entry
by , 04-26-2012 at 09:22 PM (1072 Views)
Action Listeners are the most common and easiest event handlers for implementation. An action listener is implemented to detail that what could be possibly done when cetain operations are performed by user.

When a user performs an action, an action event takes place. For example, when a button is clicked by a user, menu item is choosen and in text field ENTER is pressed. An actionPerformed message would be sent to all of the action listeners , which on a relevant component are registered.

These steps shall be followed for writing an Action Listener.

1. Event handle class shall be declared and also specifiy class which will be implementing the interface ActionListener or will be extending the class which implements interface ActionListener. E.g
2. public class, MyClass will be implementing the ActionListener {
3. For example: Event handler class instance shall be registered as listener at 1 or more components. E.g.
4. someComponent.addActionListener(instanceOfMyClass) ;
5. Code shall be included which makes the implementation of the methods present in listener interface. E.g:
6. public void actionPerformed(ActionEvent e) {
7. ...//code which will be reacting to the action...
8. }

Submit "How to Write an Action Listener" to Facebook Submit "How to Write an Action Listener" to Digg Submit "How to Write an Action Listener" to Submit "How to Write an Action Listener" to StumbleUpon Submit "How to Write an Action Listener" to Google