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

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