You will learn about the following in this chapter:
How events impact programming
How the Java Event Model works
What the Event Listener interfaces are and how they are used to capture events
Graphical User Interfaces (GUIs)were invented in the early 1980s, but they didn't start becoming the user interface of choice for application programmers until about 1990. Most programs that were written in the corporate world prior to that were character-based and menu-driven interfaces.
Menu-driven programmingis really very simple. Your application provides a menu to the user that contains several choices. The user picks one and presses Enter. Your program tests to find out which one the user picked and responds appropriately. If there are ten items on the menu, there are exactly ten possible situations that must be handled. If the user types in a character or number that is not on the menu, your program can either ignore it or provide an error message.
GUI programming is entirelydifferent. If you think about a modern windowing operating system like Microsoft Windows, Linux GUI, or the Mac OS, you will realize that there are hundreds of possible actions that the user can perform at any moment. He may close your application by closing the window, change the focus to another window, start a new application, minimize your application, move the mouse on and off your application, type something on the keyboard, or hit some shift-key combination. The list of possible user actions in a GUI is endless.
All GUI-based systems work on theprinciple of events. Events are created by the operating system whenever it notices that some signal is being generated by the hardware. This hardware is normally the keyboard or mouse, but it could also be the CPU.
In this chapter, you will learn how Java programs find out about the events that have occurred. You will also learn how to write programs that respond to these events. Finally, you will learn how to use special interfaces and classes to simplify the handling of events in Java programs.