Wednesday 2 March 2016

More Events and Interfaces

Now that we have a JButton, we need some way to communicate with it—that is, to get the events it generates. We could just listen for mouse clicks within the button and act accordingly, but that would require customization, via subclassing of the JButton, and we would be giving up the advantages of using a pre-fab component. Instead, we have the HelloComponent3 object listen for higher-level events, corresponding to button presses. A JButton generates a special kind of event called an ActionEvent when someone clicks on it with the mouse. To receive these events, we have added another method to the HelloComponent3 class:

 public void actionPerformed( ActionEvent e ) {  
 if ( e.getSource() == theButton )  
 changeColor();  
 }  
If you followed the previous example, you shouldn’t be surprised to see that HelloCom ponent3 now declares that it implements the ActionListener interface in addition to MouseMotionListener. ActionListener requires us to implement an actionPer formed() method that is called whenever an ActionEvent occurs. You also shouldn’t be surprised to see that we added a line to the HelloComponent3 constructor, registering itself (this) as a listener for the button’s action events:
 theButton.addActionListener( this );  
Note that this time, we’re registering our component as a listener with a different object—the button—whereas previously we were asking for our own events. The actionPerformed() method takes care of any action events that arise. First, it checks to make sure that the event’s source (the component generating the event) is what we think it should be: theButton. This may seem superfluous; after all, there is only one button. What else could possibly generate an action event? In this application, nothing, but it’s a good idea to check because another application may have many buttons, and you may need to figure out which one has been clicked. Or you may add a second button to this application later, and you don’t want it to break something when you do. To check this, we call the getSource() method of the ActionEvent object, e. We then use the == operator to make sure the event source matches theButton. In Java, == is a test for identity, not equality; it is true if the event source and theButton are the same object. The distinction between equality and identity is important. We would consider two String objects to be equal if they have the same characters in the same sequence. However, they might not be the same object. Once we establish that event e comes from the right button, we call our changeColor() method, and we’re finished. You may wonder why we don’t have to change mouseDragged() now that we have a JButton in our application. The rationale is that the coordinates of the event are all that matter for this method. We are not particularly concerned if the event falls within an area of the screen occupied by another component. This means you can drag the text right through the JButton: try it and see! In this case, the arrangement of containers means that the button is on top of our component, so the text is dragged beneath it.

0 comments:

Post a Comment