Monday, February 13, 2012

Implementing an event listener

To code an event handler you need to

  • declare an event handler class
  • register an instance of the class as a listener on an event source
  • include code to implement the methods
You declare an event handler class by implementing a listener interface or by extending a class that does.
Because Java allows multiple interface implementations, it's possible for a single class to implement more than one listener interface. This feature allows you to use the same class to handle different types of events.
In the example, the SearchButton class implements the MouseListener interface, so that it is capable of handling mouse events, such as mousePressed or mouseClicked.
class SearchButton extends Button implements MouseListener {
  Gui3 frame; // Refers to the parent window
  SearchButton(String caption, Gui3 frame) {
    super (caption);
    this.frame = frame;
    addMouseListener(this);
  }
}
Once you have declared the event handler class, you need to register the listener. When listeners are registered on an event source, the Java runtime system automatically invokes the correct method in the listener in response to events, using a method call from the event source.
component.addeventlListener(theListener);
Each component has a set of methods available for registering listeners - one for each event type.

In the code example, the SearchButton class registers itself to listen for mouse events using the addMouseListener method.
class SearchButton extends Button implements MouseListener {
  Gui3 frame; // Refers to the parent window
  SearchButton(String caption, Gui3 frame) {
    super (caption);
    this.frame = frame;
    addMouseListener(this);
  }
}

1 comments:

Selenium is actually a room of several automatic screening instruments, all of them catering to different tests requires.
Every one of these resources are categorized as precisely the same umbrella of open source type and supports only internet based software evaluation.
Selenium collection is comprised of SEVERAL essential elements; Selenium IDE, Selenium RC, WebDriver, Selenium Grid
. For more info on this check this link

Post a Comment