Topics in This Chapter
- Processing events in GUI controls
- Creating an image button
- Radio buttons
- Combo boxes (drop down menus)
- List boxes
- Text areas
- Creating a slider class
- Popup Menus
n the previous three chapters, we discussed the basic window types provided by the Abstract Window Toolkit (AWT) and how to process the events that occur in them. These windows differ in whether or not they have borders or can contain other graphical components. However, they are essentially similar in the way that they handle mouse and keyboard events and the way in which drawing is done (usually by overriding
paint). The GUI controls (or "widgets") discussed in this chapter are not like this. First of all, in Java 1.0, scrollbars, checkboxes, buttons, and the like do not receive the mouse and keyboard events that windows do. They do in Java 1.1, but even so, those events are not of primary concern. Instead, Java defines higher-level events based on when a button was clicked, a checkbox was checked or unchecked, a slider was adjusted, and so forth. Secondly, these GUI controls have their own graphical representation that the user should not attempt to tamper directly with; the
paint method is not used at all. Instead, they adopt the look and feel of the operating system on which they are running (via a native "peer"), leaving interfaces that allow the user to change only certain aspects of their appearance. This is a two-edged sword. On the one hand, it lets you easily adapt to multiple platforms and saves you from the myriad details that implementing GUI controls yourself would require. On the other hand, you have only limited control over these graphical objects. For instance, you can always change the labels of buttons, may or may not be able to change their color (depending on operating system), and can never change their shape, border thickness, or what constitutes clicking them. However, in many cases you can implement your own user interface control from scratch; I'll give a complete implementation of an image button to illustrate this.
Despite the promise of cross-platform support, there are a few inconsistencies in the way various features are implemented on different platforms, especially with regard to color usage. Except for scrollbars, these differences are not all that significant, but it is still important to be aware of them when developing multiplatform applications. I'll alert you to them throughout the chapter.