Page 680 - Introduction to Programming with Java: A Problem Solving Approach
P. 680

                646 Chapter 16 GUI Programming Basics
To understand this chapter, you need to be familiar with object-oriented programming, arrays, inheri- tance, and exception handling. As such, you need to have read up through Chapter 14. This chapter does not depend on material covered in Chapter 15.
16.2 Event-Driven Programming Basics
GUI programs usually use event-driven programming techniques. The basic idea behind event-driven pro- gramming is that the program waits for events to occur and the program responds to events if and when they occur.
Terminology
So what is an event? An event is a message that tells the program that something has happened. For ex- ample, if the user clicks a button, then an event is generated, and it tells the program that a particular button was clicked. More formally, when the user clicks a button, we say that the button object fires an event. Note these additional event examples:
  User Action
  What Happens
   Pressing the Enter key while the cursor is inside a text box.
  The text box object fires an event, and it tells the program that the Enter key was pressed within the text box.
    Clicking a menu item. The menu item object fires an event, and it tells the
Apago PDF Enhancer
program that the menu item was selected.
 Closing a window (clicking on the window’s top-right corner “X” button).
  The window object fires an event, and it tells the program that the window’s close button was clicked.
  If an event is fired, and you want your program to handle the fired event, then you need to create a lis- tener for the event. For example, if you want your program to do something when the user clicks a particular button, you need to create a listener for the button. For now, think of a listener as an ear. If an event is fired and there’s no ear listening to it, then the fired event is never “heard” and there’s no response to it. On the other hand, if there is an ear listening to a fired event, then the ear “hears” the event and the program then responds to the fired event. The way the program responds is by executing a chunk of code known as an event handler. See Figure 16.2. It depicts a button being pressed (see the mouse pointer), an event being fired (see the sound waves), a listener hearing the event (see the ear), and an event handler being executed (see the arrow going down the event-handler code). This system of using listeners for event handling is known as the event-delegation model—event handling is “delegated” to a particular listener.
The Event-Driven Programming Framework
Based on the above description, event-driven programming may feel like an altogether new type of program- ming. Particularly the part about firing an event and listening for a fired event. Many people are fine with the idea of event-driven programming being a new type of programming. But the truth of the matter is that it’s really just object-oriented programming with window dressing. Make that lots of window dressing. Sun pro- vides an extensive collection of GUI classes that, together, form a framework on which to build GUI applica- tions. And that framework is comprised of classes, methods, inheritance, and so on; that is, it’s comprised of OOP components. As a programmer, you don’t have to understand all the details of how the framework

















































































   678   679   680   681   682