Swing Application Shutdown
After discussing the startup in the last post I continue on the shutdown of a Java application. Typically when the application frame (or window) closes the user is asked if he really wants to exit. If he commits this the application exits while closing the application. In the other case the application might restart or do nothing. How can this be handled with Java Swing?
A common way of shutdown implementation is to add an implementation of WindowListener to the application frame and react on the events. The listener already has a callback windowClosing(WindowEvent) which is called before the window closes. Here the application might ask the user if he really wants to exit or just clicked the wrong button
. In most cases if the user commits a common implementation is to invoke System.exit(0) because this terminates the JVM but the JFrame class provides a more convinient approach. Simply set the default closing operation to JFrame.EXIT_ON_CLOSE will do the same job. If the application requires a callback that the application frame is really closed simply set the closing operation to JFrame.DISPOSE_ON_CLOSE and implement the listeners windowClosed(WindowEvent) method. You even don’t need to invoke System.exit(int) because the EDT exits when no window is active. Which means if the application frame is the only window it also exits the application.
This image shows the event invocations when a window is closed. First the user closes the window by pressing the red icon in the top right corner. This causes the EDT to dispatch a window event to the application frame. The event has the type WindowEvent.WINDOW_CLOSING. The application has registered a WindowListener that invokes shutdown in the EDT. In the shutdown method the application asks the user if he really wants to exit. If he aborts the application sets the default close operation to JFrame.DO_NOTHING_ON_CLOSE which continues the application and keeps the frame open. If the user commits the application sets the default close operation to DISPOSE_ON_CLOSE which causes the application frame to be disposed. This again causes the EDT to dispatch a new WindowEvent with the type WindowEvent.CLOSED. Now the application can do some magic in the EDT after the window is closed. If no window exists the EDT exits and the JVM terminates. If the application sets the default close operation to JFrame.EXIT_ON_CLOSE the application will terminate immediately after the windowClosing method is executed (just look at the code of JFrame’s processWindowEvent method).
Swing Application Startup
The startup of an application is one of the based parts where a framework support is very helpful because especially with Java Swing. In Swing there is the Event Dispatch Thread (EDT) which executes any UI related stuff. So anything that is related to UI should be executed in this thread, even the creation of a frame. So how can a framework support this procedure. One example comes with the Swing AppFramework where a launcher moves the creation and invocation of the startup method into the EDT but than also does a lot of application specific or sometimes unneeded stuff like I18n, session storage.
A really simple approach is shown in the following picture.
The user starts the application either using Web Start, clicking on the JAR or command line. In the applications main-method is one line Launcher.launch(Application.class, args) where the application class and command-line arguments are passed to the launcher. This one creates a new application instance and invokes the startup method in the EDT using SwingUtilities.
Because no window is shown any configured splash screen will be shown until the frame (or window) which is created in startup method is shown to the user. The behavior of startup is application specific but often it can be divided into an initialization part of the platform. Typical services like executor, communication layers, logging, modularization are initialized. After this is completed the application frame is created and shown to the user.
Java 6 Splash-Screen
Working on Java Swing is sometimes not the easiest! Today I came across a problem with the Java6 Splash-Screen feature where you can define the splash screen in the manifest file. I wanted to see when the splash screen gets shown and when it gets disposed. The screen really gets shown when the JVM starts, so its good to use it for showing feedback while initialization. And the screen gets disposed as soon as the first window, may it be a frame or dialog, gets shown.
But nothing comes for free, a problem occured as I added class-path entries to the manifest. The splash screen was not shown! So I played around with a dozen of combinations in the manifest file. The outcome is that if the Splashscreen-Image entry is after the Class-Path entry no splash screen will be shown! So always keep you entry for the splash screen at the top of the manifest file.
Learning Adobe Flex
With the existing stream of building rich internet applications (RIAs) I also have to look at this technology. So I started learning Adobe Flex because after some talk with collegues and looking around it looks like this is the most stable and widely used technology. JavaFX is just starting and has still a long way to go and Silverlight just only runs on Windows machines with IE installed.
So the first I had to do was browsing the documentation at Adobe Open Source and getting a feeling of Flex. This means mainly downloading the SDK, looking at the examples and fiddling around with them. The concept with declaring the user interface in XML is nice and makes it really easy building simple forms and screens. With the mechanism of having an id-map in the application from where the application code, UI and everything gets and displays the entered/computed text works well. Also with ActionScript which is like JavaScript but with just a little bit more type information makes it also easier for a Java developer to dig into it. One of the biggest points is the extensive API documentation. The structure with having an example available for each UI component and function is quite charming. I hope to see this kind of documentation also in some other projects.
As an example I’d written a small application to calculate the real inflation adjusted return for a one-year cash-investment in a CD.

