Event handling is of fundamental importance to programs with a graphical user interface. To implement graphical user interfaces, you must master how Java handles events. This chapter explains how the Java AWT event model works. You will see how to capture events from the mouse and the keyboard. This chapter also shows you how to use the simplest GUI elements, such as buttons. In particular, this chapter discusses how to work with the basic events generated by these components. The next chapter shows you how to put together the most common of the components that Swing offers, along with a full coverage of the events they generate. The current Java event model is different from the one used in Java 1.0. If you are already familiar with Java 1.0, you will need to learn a new way of coding: one that bears little, if any, relation to the way you used to do things. Note, however, that while Sun has said that code written following the older event model should still work in later versions of Java, moving to the new event model promises both performance improvements and a greater degree of flexibility. We only briefly discuss the older event model in this chapter. You can tell the compiler to flag all lines that use one of the older event handling methods by compiling your source code with the -deprecation switch. (There is some potential for incompatibility down the road. Sun has not said they will be supporting the older event model indefinitely. And, of course, if you use Swing components, then you must use the new event model.) If you need to write applets that can run under old browsers that are still using Java 1.0, such as Netscape Navigator 2 or 3 or Internet Explorer before version 4, then you must use the Java 1.0 event model. | |