<< 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.

Questions & Answers

Biology is a branch of Natural science which deals/About living Organism.
Ahmedin Reply
what is phylogeny
Odigie Reply
evolutionary history and relationship of an organism or group of organisms
AI-Robot
ok
Deng
what is biology
Hajah Reply
cell is the smallest unit of the humanity biologically
Abraham
what is biology
Victoria Reply
what is biology
Abraham
HOW CAN MAN ORGAN FUNCTION
Alfred Reply
the diagram of the digestive system
Assiatu Reply
allimentary cannel
Ogenrwot
How does twins formed
William Reply
They formed in two ways first when one sperm and one egg are splited by mitosis or two sperm and two eggs join together
Oluwatobi
what is genetics
Josephine Reply
Genetics is the study of heredity
Misack
how does twins formed?
Misack
What is manual
Hassan Reply
discuss biological phenomenon and provide pieces of evidence to show that it was responsible for the formation of eukaryotic organelles
Joseph Reply
what is biology
Yousuf Reply
the study of living organisms and their interactions with one another and their environment.
Wine
discuss the biological phenomenon and provide pieces of evidence to show that it was responsible for the formation of eukaryotic organelles in an essay form
Joseph Reply
what is the blood cells
Shaker Reply
list any five characteristics of the blood cells
Shaker
lack electricity and its more savely than electronic microscope because its naturally by using of light
Abdullahi Reply
advantage of electronic microscope is easily and clearly while disadvantage is dangerous because its electronic. advantage of light microscope is savely and naturally by sun while disadvantage is not easily,means its not sharp and not clear
Abdullahi
cell theory state that every organisms composed of one or more cell,cell is the basic unit of life
Abdullahi
is like gone fail us
DENG
cells is the basic structure and functions of all living things
Ramadan
What is classification
ISCONT Reply
is organisms that are similar into groups called tara
Yamosa
Got questions? Join the online conversation and get instant answers!
Jobilize.com Reply

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