<< Chapter < Page Chapter >> Page >

Listing 7 begins by instantiating a new Button object.

(Note that with this event model, it is not necessary to extend the Button class, because it is not necessary to override methods belonging to the Button object.)

Register a MouseListener object

After instantiating the Button object, the code in Listing 7 calls the addMouseListener method to register a MouseListener object on that button. The argument to the addMouseListener method must be a reference to an object instantiated from a class that implements the MouseListener interface.

Instantiate the listener object

In this case, that listener object is created by

  • writing an expression to instantiate an anonymous object from an anonymous class and
  • placing that expression as an argument to the addMouseListener method.

Implement the MouseListener interface

The definition of the anonymous class in this example uses the syntax thatimplements an interface.

An instance initializer

As before, an instance initializer is used to get and display the name of the class file thatrepresents the anonymous class. Thus, when the new anonymous object of the anonymous class is instantiated, the text shown in Figure 7 appears on the screen.

Note the similarity of this class file name to that shown earlier in Figure 6 . (You can also compare them in Figure 4 .) The names of the two class files differ only with respect to a number that is provided by the compiler to guarantee that each class file name isunique.

Implementing the interface

Whenever a class implements an interface, it must provide a concrete definition for each of the methods declared in theinterface, even if some of those methods are empty.

Continuing with the definition of the anonymous class, Listing 8 provides definitions for all five of the methods declared in the MouseListener interface. Four of those methods are defined as empty methods.

Separation of event subcategories

One of the major differences between the low-level event model discussed earlier and the source-listener model being discussed here has to do with where the separation between the differentsubcategories (mouseClicked, mousePressed, mouseReleased, etc.) of a given event type is accomplished.

In the low-level model, the separation must be accomplished by code in the overridden event handler method, such as with the if statement in the processMouseEvent method defined in Listing 6 .

In the source-listener model, the separation is accomplished before the eventhandler method is called, and a specific event handler method, such as the mouseClicked method is called on the listener object.

When the button fires a mouse event ...

In this case, whenever the button fires a MouseEvent of the MOUSE_CLICKED subcategory , the mouseClicked method defined in Listing 8 will be called automatically, causing the seventh line of text in Figure 4 to appear on the screen.

Whenever the button fires a MouseEvent of one of the other subcategories , one of the empty methods defined in Listing 8 will be called. This method will return immediately, doing nothing butwasting a little computer time.

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