Step 5

In this step, we will process scrollbar events. Recall from the earlier discussion that the handelEvent method in the Component class handles all events except for those that it sends off to an action method. Since there is not such method for scrollbars, they are handled within the handleEventmethod, itself. Since the method in the Component class doesn't do anything useful with events, we must override the Component class handleEvent method.

The following block of code does this; as above, the only "processing" we do is to print out a message identifying the event that was handled. In an real application, your program would include code to actually process the scrollbar events.

  public boolean handleEvent ( Event event )  {

  if (  (event.target instanceof Scrollbar) && ( (event.id == 
  Event.SCROLL_ABSOLUTE) || 
  (event.id == Event.SCROLL_LINE_DOWN)  || 
  (event.id == Event.SCROLL_LINE_UP) || 
  (event.id == Event.SCROLL_PAGE_DOWN) || 
  (event.id == Event.SCROLL_PAGE_UP) )  )  {

    if ( event.target == scrollbarV )  {
System.out.println ( "scrollbarV:  y = "+((Integer)event.arg).intValue() );
    return true;
    }  // end scrollbarV

    if ( event.target == scrollbarH )  {
System.out.println ( "scrollbarH:  x = "+((Integer)event.arg).intValue() );
    return true;
    }  // end scrollbarH

    return super.handleEvent ( event );
  }  // end both scrollbars

    return super.handleEvent ( event );
  }  // end handleEvent


If you ran the applet with these two methods added, you would discover, unfortunately, that it does nothing. Why?

The reason has to do with the classes that receive the events and their locations within the class hierarchy relative to our handleEvent method within our Applet. We will solve this problem in the next step.