Action Listener Event in Advanced Java Programming

Home  »  Java  »  Advance Java »    Java Event Handling »    ActionListener

The ActionListener interface declares a method that is called when an action event is generated. The method defined in ActionListener interface is a single method, voic actionPerformed(ActionEvent ae). You use the ActionListener interface when you handle events such as clicking AWT buttons. The addActionListener() method enables tracing a button object for the occurrence of an action event. The following program code shows how to implement the ActionListener interface:

Implementing ActionListener Interface:


The above listing shows implementing the ActionListener with the instance of AWT Button class, Border Layout is set to the applet window and four button objects. RED, BLUE, GREEN and YELLOW are created. When you click a button the colour of background of the window changes to the colour of the button label and code will be stored in the file called ActionListenerTest.java, which on compilation will crate a class file called ActionListenerTest.class. Now compile this file using below commands in the DOS mode:

C:\>jdk1.4\bin>javac ActionListenerTest.java
C:\>jdk1.4\bin>appletviewer ActionListenerTest.java


"Output of ActionListenerTest.java"


After clicking on the button the output...

"Output of ActionListenerTest.java"


Download Complete Program

  -  



Event Listener


Action Listener Adjustment Listener Mouse Listener
Mouse Motion Listener Window Listener Key Listener
Focus Listener ITEM Listener Text Listener



No comments:

Post a Comment