Java Reference
In-Depth Information
Tip Adding a shutdown hook is a good way of handling shutdowns within an application that will be run-
ning on a server. No matter whether your code calls System.exit , or the user clicks the close button, or the
application is told by the operating system that it must shut down, the same hook will be run. Handling the
cases where the operating system tells the application to shut down is especially valuable—consider the
case where the server is running on an uninterruptible power supply (UPS) and the power goes out. Normally
the UPS runs for a while on battery power, but that can only last so long. So before the battery is likely to run
out, the UPS sends a message to the operating system telling it to shut down. The operating system then
sends a message to all running applications telling them to shut down. The shutdown hook allows your
application to receive this message and perform a clean shutdown.
â– 
Listing 8-32. The Shutdown Hook Code
package sampleproject.gui;
import java.io.IOException;
import java.util.logging.*;
import sampleproject.db.*;
public class CleanExit extends Thread {
private Logger log = Logger.getLogger("sampleproject.gui");
public void run() {
log.info("Ensuring a clean shutdown");
try {
DVDDatabase database = new DVDDatabase();
database.setDatabaseLocked(true);
} catch (IOException fne) {
log.log(Level.SEVERE, "Failed to lock database before exiting", fne);
}
}
}
Swing Changes in J2SE 5
Sun has introduced several changes to Swing for the latest release of J2SE. Several of these
improvements can be used to provide a more polished submission. We will briefly discuss
them in this section.
Improve Default Look and Feel of Swing
Prior to JDK 5, if you wanted to provide a common look and feel for your application on multi-
ple platforms, you had to use the Metal look and feel, which by default used to look like
Figure 8-37.
Search WWH ::




Custom Search