<< Chapter < Page Chapter >> Page >

The source-listener event model that I will illustrate later doesn't suffer from that restriction.

An anonymous inner class for low-level event handling

Listing 5 shows the beginning of an anonymous class to perform low-level event handling similar tothat shown in Listing 4 .

This code defines an anonymous inner class that implicitly extends Button and has mouse events enabled. I provided this class primarily for comparison with the local class named BaldButton . This class is an anonymous alternative to the local BaldButton class.

An argument to the add method

Note that the definition of this anonymous class appears as an argument to the add method for the frame. Thus, the anonymous object instantiated from the anonymous class is added as thesecond (middle) button in Figure 2 .

Extends the Button class

Note also that this form of anonymous class implicitly extends the Button class. Once again, this makes it possible to override the processMouseEvent method belonging to the Button class.

An instance initializer

As I mentioned earlier in this module, it is not possible to define a constructor for an anonymous class. However, it is possible to define aninstance initializer.

(In Listing 5 , the instance initializer consists of two statements enclosed by matching curly brackets.)

This class defines an instance initializer that

  • Enables mouse events on an anonymous object instantiated from the anonymous class.
  • Gets and displays the name of the class file that represents the anonymous class.

The screen output

Therefore, the instantiation of this anonymous object causes the text shown in Figure 6 to appear on the screen. About all you can tell by looking at this class name is that it is the name of a file thatrepresents an anonymous class.

Overridden processMouseEvent method

The remaining code in the anonymous class definition is shown in Listing 6 . The code in Listing 6 consists of

  • an overridden processMouseEvent method, plus
  • the curly brackets, parentheses, and semicolon necessary to complete the expression and the statement.

Same code as before

The code in this overridden processMouseEvent method is essentially the same as that shown for the local class in Listing 4 , except that it produces a different message on the screen when the user clicks the button.

Clicking the middle button in Figure 2 produces the screen output shown by the sixth line in Figure 4 .

Instantiating and registering a MouseListener object

Implementing a listener interface

Now I'm going to switch from low-level event handling to source-listener event handling. The code to accomplish this begins in Listing 7 .

With this event handling model:

  • A listener object is instantiated from a class that implements a specific listener interface. In this case, that interface will be the MouseListener interface.
  • The listener object is registered on an object that knows how to fire events of a type that is associated with the listener interface. Inthis case, that will be events of type MouseEvent .
  • When the source object fires an event of the specified type, one of the methods belonging to the registered listener object will be called to handlethe event. The different methods belonging to the listener object are declared in the implemented listener interface.

Get Jobilize Job Search Mobile App in your pocket Now!

Get it on Google Play Download on the App Store Now




Source:  OpenStax, Object-oriented programming (oop) with java. OpenStax CNX. Jun 29, 2016 Download for free at https://legacy.cnx.org/content/col11441/1.201
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'Object-oriented programming (oop) with java' conversation and receive update notifications?

Ask