<< Chapter < Page Chapter >> Page >

If you are unfamiliar with the enableEvents method, you should look it up in the Sun documentation. Briefly, this method must be called on the button to cause theoverridden processMouseEvent method to be called later when the button fires a mouse event.

The remaining constructor code

The remaining code in the constructor

  • Sets the text value on the face of the button
  • Gets and displays the name of the class file that represents this local class

The screen output

Construction of the button by the code in Listing 3 causes the text shown in Figure 5 to appear on the screen. This is how I was able to identify the name of the class file that represents the local classin my earlier discussion of class file names.

We will see later that this button will be added as the leftmost button in the GUI shown in Figure 2 .

The processMouseEvent method

Continuing with the constructor for the GUI class, Listing 4 shows the overridden processMouseEvent method for an object of the BaldButton class.

This method is called each time an object instantiated from this class fires a mouse event. That is why I refer to the method as an event handler forthe button.

Different kinds of mouse events A button can fire a variety of different kinds or subcategories of mouse events :

  • MOUSE_CLICKED
  • MOUSE_DRAGGED
  • MOUSE_ENTERED
  • MOUSE_EXITED
  • MOUSE_MOVED
  • MOUSE_PRESSED
  • MOUSE_RELEASED

In this case, I elected to ignore all but MOUSE_CLICKED . This subcategory of mouse event occurs when a mouse button is pressed and thenreleased.

The code in the event handler of Listing 4 first confirms that the event was of the MOUSE_CLICKED variety. If so, it displays a messagethat matches the fifth line of text in the output shown in Figure 4 .

Call processMouseEvent on the superclass

Without getting into the details of why this is required, I'm simply going to tell you that when you use this low-levelevent model to handle events, your overridden processMouseEvent method must call the same method in the superclass, passing the incoming parameter oftype MouseEvent as a parameter to the superclass version of the method.

Add a button to the frame

The last statement in Listing 4 instantiates a new BaldButton object, setting the text on the face of the button to A , and adds that new object to the frame.

Because the layout property of the frame has been set to FlowLayout , and because this is the first component added to the frame, this button appears as the leftmost button in the GUI shownin Figure 2 .

Could instantiate multiple buttons of this type

Although I instantiated the button object as an anonymous object in this case, that wasn'tnecessary. Using this local class, I could instantiate more than one object of this type, saving the object's references in reference variables ofthe appropriate type. Later we will see that this is not possible for anonymous classes.

It is interesting to note, however, that with this eventhandling model, if I were to instantiate multiple buttons of this type, the same processMouseEvent method would be called no matter which of the buttons fired a mouse event. If I wanted different behavior as a result of thedifferent buttons firing mouse events, I would have to write code inside the processMouseEvent method to deal with that issue.

Questions & Answers

what is microbiology
Agebe Reply
What is a cell
Odelana Reply
what is cell
Mohammed
how does Neisseria cause meningitis
Nyibol Reply
what is microbiologist
Muhammad Reply
what is errata
Muhammad
is the branch of biology that deals with the study of microorganisms.
Ntefuni Reply
What is microbiology
Mercy Reply
studies of microbes
Louisiaste
when we takee the specimen which lumbar,spin,
Ziyad Reply
How bacteria create energy to survive?
Muhamad Reply
Bacteria doesn't produce energy they are dependent upon their substrate in case of lack of nutrients they are able to make spores which helps them to sustain in harsh environments
_Adnan
But not all bacteria make spores, l mean Eukaryotic cells have Mitochondria which acts as powerhouse for them, since bacteria don't have it, what is the substitution for it?
Muhamad
they make spores
Louisiaste
what is sporadic nd endemic, epidemic
Aminu Reply
the significance of food webs for disease transmission
Abreham
food webs brings about an infection as an individual depends on number of diseased foods or carriers dully.
Mark
explain assimilatory nitrate reduction
Esinniobiwa Reply
Assimilatory nitrate reduction is a process that occurs in some microorganisms, such as bacteria and archaea, in which nitrate (NO3-) is reduced to nitrite (NO2-), and then further reduced to ammonia (NH3).
Elkana
This process is called assimilatory nitrate reduction because the nitrogen that is produced is incorporated in the cells of microorganisms where it can be used in the synthesis of amino acids and other nitrogen products
Elkana
Examples of thermophilic organisms
Shu Reply
Give Examples of thermophilic organisms
Shu
advantages of normal Flora to the host
Micheal Reply
Prevent foreign microbes to the host
Abubakar
they provide healthier benefits to their hosts
ayesha
They are friends to host only when Host immune system is strong and become enemies when the host immune system is weakened . very bad relationship!
Mark
what is cell
faisal Reply
cell is the smallest unit of life
Fauziya
cell is the smallest unit of life
Akanni
ok
Innocent
cell is the structural and functional unit of life
Hasan
is the fundamental units of Life
Musa
what are emergency diseases
Micheal Reply
There are nothing like emergency disease but there are some common medical emergency which can occur simultaneously like Bleeding,heart attack,Breathing difficulties,severe pain heart stock.Hope you will get my point .Have a nice day ❣️
_Adnan
define infection ,prevention and control
Innocent
I think infection prevention and control is the avoidance of all things we do that gives out break of infections and promotion of health practices that promote life
Lubega
Heyy Lubega hussein where are u from?
_Adnan
en français
Adama
which site have a normal flora
ESTHER Reply
Many sites of the body have it Skin Nasal cavity Oral cavity Gastro intestinal tract
Safaa
skin
Asiina
skin,Oral,Nasal,GIt
Sadik
How can Commensal can Bacteria change into pathogen?
Sadik
How can Commensal Bacteria change into pathogen?
Sadik
all
Tesfaye
by fussion
Asiina
what are the advantages of normal Flora to the host
Micheal
what are the ways of control and prevention of nosocomial infection in the hospital
Micheal
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