Mastering Java: An Effective Project Based Approach including Web Development, Data Structures, GUI Programming and Object Oriented Programming (Beginner to Advanced) by White Michael B

Mastering Java: An Effective Project Based Approach including Web Development, Data Structures, GUI Programming and Object Oriented Programming (Beginner to Advanced) by White Michael B

Author:White, Michael B. [White, Michael B.]
Language: eng
Format: azw3, epub
Published: 2020-05-21T16:00:00+00:00


}

Next, a list of the XxxEvent listener objects must be maintained in the source and two methods defined – addXxxListener() and removeXxxListener. These will, respectively, add and remove a listener to or from the list. The method signatures are:

public void addXxxListener(XxxListener lis);

public void removeXxxListener(XxxListener lis);

Note; addXxxListener() will take just one parameter and that will be an XxxListener object. That means the only objects it can add are those of the XxxListener type and sub-type. Because XxxListener is an interface, there is no way to create an instance of it but, instead a subclass instance can be created to implement that interface.

To summarize, the source is identified, along with the listener object and the event-listener interface. The listener then implements that interface and the source object will register the listener object using the addXxxListener() method.

The user will trigger the source and an XxxEvent object is created; this will capture all the information needed about the activation. Lastly, the source will invoke the right handler for each of the listeners in the list and programmed response will be triggered.

Examples

AWTCounter – ActionListener Interface and ActionEvent

When a user clicks on a Button or presses the Enter key on a TextField, an ActionEvent signal is sent to all the related ActionEvent Listeners. These listeners will them implement the ActionListener Interface and this, in turn declares a single abstract method which is called actionPerformed(). You can see this below:

public interface ActionListener {

public void actionPerformed(ActionEvent evt);

// Called back on button-click (on Button), enter key pressed (on TextField)

}

The steps for the event-handling are:

btnCount() is identified as the source object

An ActionEvent is sent to all related ActionEvent listeners when a Button is clicked

The listener implements the ActionListener interface and overrides the method called actionPerformed() to get the correct response. For the sake of simplicity, we have used an object called “this” (an AWTCounter) as the ActionEvent listener. As such, the “this” class must implement the interface and provide the response that actionPerformed() is programmed to provide:

public class AWTCounter extends Frame implements ActionListener {

// "this" is the ActionEvent listener, as such, it must

// implement ActionListener interface

......

// Implementing ActionListener interface requires this class to provide implementation

// to the abstract method actionPerformed() declared in the interface.

@Override

public void actionPerformed(ActionEvent evt) {

// Programmed response upon activation

// Increment the count value and display on the TextField

++count;

tfCount.setText(count + "");

}

}

The method called addActionListener() is used by the source object to register the listener; in the above example, btnCount(Button) adds the object called “this” as a listener, like this:

btnCount.addActionListener(this);

Note; addActionListener() will take one argument and that will be of ActionListener type. The object, “this”, which is implementing the addActionListener which is, in turn a subclass of ActionListener, gets passed up to the method called addActionListener().

When a button is clicked, btnCount will create the ActionEvent object and the actionPerformed() method for all the related listeners is called back by the ActionEvent object:

ActionEvent evt = new ActionEvent( ...... );

listener.actionPerformed(evt); // for all its listener(s)

AWTAccumulator – ActionEvent and ActionListener Interfac e

In the example from before:

The source object is identified as tfInput(TextField)

When



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.