Event Handling Java

Next question is how the parameter "this" refers the ActionListener only? Why not your own program object ButtonDemo, or a Frame or ActionEvent?

You are right cent percent to think so. Then how to answer the above question. For the answer, we must refer the method signature of addActionListener() method as defined in Button class. Following is the one.

      public synchronized void addActionListener(ActionListener);

Observe, the parameter must be an object of ActionListener. When we pass "this" to addActionListener() method, the JVM tries to convert "this" into an object of ActionListener because the method demands it. The JVM finds it, in the class declaration, after implements keyword and simply links btn to ActionListener. To have a proof, just remove "implements ActionListener" and compile the program, the program does not compile.

The whole process can be put diagrammatically as follows.


This type of event handling mechanism designed by the designers follows "Observer design pattern". The ActionListener observes always the button when it gets clicked. This type of event handling model is known as "delegation event model". In this model, responsibility of event handling is delegated (assigned) to listeners.

26 thoughts on “Event Handling Java”

  1. Hi sir how to execute the above program(ButtonDemo program) when i am executing it is giving this error “Exception in thread “main” java.lang.NoSuchMethodError: main”

  2. sir,
    i am a student of computer science…. this website is more helpful and useful for me. because tomorow i have seminar about event handling in my class..so only…thank you sir.

  3. Easiest way of explanation..thanks alot sir ..sir if i start going through to your all the contents of java on this site..Is it enough to be a good java programmer ??

  4. Sir,How to write a program so that a new window is opened by pressing F2 key. I have tried but it is not working.I have used window and key events.

  5. Nice tutorial sir. I’s wondering if you could recommend us some of your particular favourite books of java.

    Thanks again for the tuto. I really appreciated it.

    1. Now-a-days, Internet is the best source of knowledge. So, don’t on depend on books. If you purchase one book today, tomorrow it will be obsolete. For example, if you purchase to day a Java text book, next month September, Java 8 is getting released. Then you purchase another book also.

      Tell your friends also to derive knowledge from this web site.

  6. sir

    i have inserted 3 labels in rectangle using paint method. my intention is particular label have to be moved within a rectangle when i drag it by using mouse in java

  7. Hi Sir,

    Your way of teaching is very good. it would be better for us , if you upload some your video tutorials on Java.
    Apache POI is missing in your website. Please provide some good understanding of java Apache POI . Thanks in advance.


  8. sir i want to know that…. is it possible to have different actionlistener code for two or more buttons in a class.

    1. See this code.

      import java.awt.* ; import java.awt.event.* ; import javax.swing.* ;
      public class ProgressiveTest {
      public static void main ( String args[ ] ) {

      JFrame frame = new JFrame( “Swing’s ProgressMonitor” ) ;
      JButton button = new JButton( “Start Iteration” ) ;
      frame.getContentPane().add( button, BorderLayout.CENTER );

      int min = 0 ; int max = 100 ;
      String[ ] message = new String[ 2 ] ;
      message[ 0 ] = “Performing Iterations.” ;
      message[ 1 ] = “Wait for completion…….” ;
      final ProgressMonitor monitor = new ProgressMonitor(frame, message, “Iteration”,
      min, max ) ;
      final Runnable runnable = new Runnable( ) {
      public void run( ) {
      int sleepTime = 500 ;
      for( int i = 1 ; i < 100 ; i++ ) { try { monitor.setNote( "Iteration " + i ) ; monitor.setProgress( i ) ; if ( monitor.isCanceled( ) ) { monitor.setProgress( 100 ) ; break ; } Thread.sleep( sleepTime ) ; } catch ( InterruptedException e ) { } } monitor.close( ) ; } } ; button.addActionListener( new ActionListener( ) { public void actionPerformed( ActionEvent event ) { // run the iterations in a separate thread Thread thread = new Thread( runnable ); thread .start( ) ; } } ) ;

  9. Dear Sir
    I’m doing project for school.
    I have two sets of 4 buttonsa1a,2a,3a,4a and 1b,2b,3b,4b. I’m trying to write if script for condition: if 1a .clicked && 2b .clicked go to page2 in my site.

    Any suggestions??
    Thank you

Leave a Comment

Your email address will not be published.