Handling Action Events
Each ofDownloadManager’s GUI controls registers anActionListenerthat invokes its
respective action method.ActionListeners are triggered each time an action event takes
place on a GUI control. For example, when a button is clicked, anActionEventis generated
and each of the button’s registeredActionListeners is notified. You may have noticed
a similarity between the wayActionListeners work and the Observer pattern discussed
earlier. That is because they are the same pattern with two different naming schemes.
Compiling and Running the Download Manager
CompileDownloadManagerlike this:
javac DownloadManager.java DownloadsTableModel.java ProgressRenderer.java Download.java
RunDownloadManagerlike this:
javaw DownloadManager
The Download Manager is easy to use. First, enter the URL of a file that you want to
download in the text field at the top of the screen. For example, to download a file called
0072229713_code.zip from the McGraw-Hill web site enter
http://books.mcgraw-hill.com/downloads/products/0072229713/0072229713_code.zip
This is the file that contains the code for my bookThe Art of Java, which I co-authored with
James Holmes.
After adding a download to the Download Manager, you can manage it by selecting it
in the table. Once selected, you can pause, cancel, resume, and clear a download. Figure 33-2
shows the Download Manager in action.
Chapter 33: Creating a Download Manager in Java 989
FIGURE 33-2 The Download Manager in action