Java Reference
In-Depth Information
Chapter 4
More GUI and the Visual Editor
This chapter will explore GUI components in more depth. We will introduce the concepts behind listeners and event
driven programming. We will then, show how to implement listeners, use them to solve the “window closing” problem
(from the previous chapter), and enable buttons to perform functions. In addition, we will show, in more detail, the
advantages of inheritance (i.e., specialization), especially how it decreases the amount of source code that must
be written by the programmer. Finally, we will introduce RAD's Visual Editor (VE). VE is a very productive tool for
generating GUI classes. Through an extensive tool bar and drag and drop, VE provides a GUI for defining GUIs!
At the end of the chapter, you should understand:
Window listeners and action listeners
How to tie listeners to GUI components
How and when to use specialization and composition
The Visual Editor's coding standards
At the end of the chapter, you should be able to:
Efficiently use superclass/subclass relationships
Use the Visual Editor to build a GUI
Enable the close button
Use buttons to perform functions within an application
Listeners
We still have a big problem with our application: the close button does not work. If we installed the application on
a PC, we would have to work with the Task Manager to shut the application down! This is very poor programming.
There must be a better way, and there is—a listener.
Listeners are classes that implement a listener interface and that are added to visual components. In other words,
just as a label is added to a frame, listeners (nonvisual classes) can be added to visual objects like a Frame or Button .
You can think of a listener as a sentry. If someone approaches, the sentry asks for their ID. When the sentry hears a
sound in the distance, he shouts, “Who goes there?” If no one responds, he fires a round in the air. Notice that the
sentry performs different functions based on different events (or actions).
In our case, when the user performs a certain action, a specific method in the listener class will be invoked.
In other words, specific methods will be called when the user clicks the close window button, when the window
is opened, when a radio button is clicked, and so on. There are two catches:
The programmer must write these methods.
There are different types of listeners for different GUI components.
 
Search WWH ::




Custom Search