However, page 17-4 of the ProgramLive CD has footnotes that discuss:
1. Listening to a return/enter key-press in a text field
2. Listening to a JList
3. Listening to a JSlider
4. Listening to a JColorChooser
5. Listening to a JComboBox
6. Listening to a JCheckBox
7. Listening to a RadioButton
Further, the ProgramLive CD contains programs that demo most of these
tasks. They can be obtained from the CD from footnotes on lesson page 17-4.
If you are designing a GUI that requires listening to one of the above com-
ponents, obtain the corresponding demo program from the ProgramLive CD and
use it as an example.
Using several listeners
A prime goal in designing a program is to place distinct tasks in different mod-
ules, thus separating your concerns . Appropriately isolating different tasks can
simplify development, debugging, and later maintenance. For example, when
changing the program later because of a change in specification, it will be easi-
er to identify the modules that need changing and change them.
The system for listening to events in Java helps us isolate different tasks in
different modules (in this case, different listeners). It is possible to register sev-
eral listeners for the same event, and all of them will be called when that event
happens — in the reverse order in which they were registered .
Activity 17-4.5 of the ProgramLive CD describes a program that has three
listeners for a button press or a return/enter key-press in a text field. When the
button is pressed or the return/enter key is pressed, three things happen:
1. An upper-case version of the text field is appended to a text area.
2. The text field is appended to an output file.
3. The text field is cleared.
Obtain the class
Each of these tasks is handled by a different listener —but each listener
responds to the same event.
In some situations, it is useful to pop up a window that asks the user for some
information, wait for the information, and then proceed depending upon what the
information is. Section 5.7.2 shows how to use a JFileChooser to request a file
to read/write from the user. Here, we look at class JOptionPane , which provides
some standard dialog windows, and class JDialog , which you can use to build
your own dialog window from scratch.