1. | What is the base class for all events? |
|
2. | What is the base class for all listeners? |
|
3. | What are the six steps to handing an event? |
|
4. | Explain the difference between a low-level event and a semantic event. |
|
5. | Why is it preferable to listen for semantic events? |
|
6. | What methods does the java.util.EventObject class define? |
|
7. | What methods does the java.util.EventListener interface declare? |
|
8. | What are the benefits and drawbacks to using the “Existing Class” approach to handling an event? |
|
9. | What is an inner class? What are the benefits to using an inner class to handle an event? |
|
10. | What is an anonymous class? What are the benefits to using an anonymous class to handle an event? |
|
11. | What is an adapter? Why might you use an adapter to handle an event? |
|
Answers
1. | - java.util.EventObject is the base class for all events. |
2. | - java.util.EventObject is the base class for all events. |
3. | - The six steps to handling events are:
|
4. | - Low-level events represent window-system occurrences or low-level input, such as the pressing of a mouse but-ton or the typing of the letter ‘a’. Semantic events include anything else like the selection of a menu or the resizing of a window, and they may or may not be triggered by user input. |
5. | - Generally speaking, semantic events are the ones to pay attention to because they represent more “meaningful” concepts. |
6. | - java.util.EventObject defines the one method getSource() which returns the source of the event |
7. | - java.util.EventListener declares no methods! |
8. | - Using an existing class is perhaps the most efficient way to approach the implementation of an event listener as there is always a bit of overhead with the loading of new class type. However, it can lead to long method definitions, and it tends to clump lots of code together that might be more elegantly expressed separately. |
9. | - An inner class is a class defined within the scope of another class. Many times, an EventListener class needs access to fields or methods of an object that would best be left private or protected. If, as is often the case, that listener will only ever be created or referenced by that same object, then it may be preferable to declare the listener as an inner class of the object, thereby giving it automatic access to all the object’s fields and simplifying access issues. |
10. | - An anonymous class is a class that cannot be referred to - not because of visibility issues but because it is never assigned a name. Use of an anonymous class is a concise and frequently appropriate design approach because the listener instance is defined in the very same place as it is used. |
11. | - Sometimes the situation arises where you need to implement an EventListener interface but are only interested in some - not all of its methods. An adapter class implements all an interface’s methods with do-nothing methods thereby providing you the option of extending it and only overriding the methods in which you are interested. Adapters are useful mostly when you are creating anonymous listener instances and want to keep the code succinct for readability purposes. |