Java Reference
In-Depth Information
always display the window again later by calling setVisible() with an argument
of true .
Of course, you might want to take some action beyond the options I have discussed here when the user
chooses to close the window. If the program involves entering a lot of data for instance, you may want
to ensure that the user is prompted to save the data before the program ends. This involves handling
an event associated with the Close menu item or the Close button, and you investigate this in the next
Creating and Displaying the Application Window
The setVisible() method with the argument set to true displays the application window on top of any
other windows that are currently visible on the screen. If you want to hide a window at some point during
the execution of an application, you would call setVisible() with the argument set to false .
The createWindow() method is called by the run() method in the anonymous Runnable class object
that is created in main() . The SwingUtilities class that is defined in the javax.swing package con-
tains several static utility methods, including the invokeLater() method that you call here. This method
executes the run() method for the Runnable object that you pass as the argument on the event dispatch-
ing thread, which is exactly what you want if you are to avoid problems with handling user interactions
with the application window.
Window Look-and-Feel
The application displays the default look-and-feel on my system, and it may well be the same on yours. It
corresponds to the Java cross-platform look-and-feel that is distributed with the JDK. Each look-and-feel
is defined by a class, and on my system the following look-and-feel classes are installed:
The first class corresponds to the standard Java look-and-feel, which used to be known as the Metal look-
and-feel. It is intended to provide a uniform cross-platform look-and-feel, and you can use it on any plat-
form that supports the JFC. The second is the Nimbus look-and-feel that is implemented using Java 2D
vector graphics and has a very small footprint. The third class defines the Motif look-and-feel that is for
use on UNIX systems. The last two classes can be used only with Microsoft Windows.
The UIManager class that is defined in the javax.swing package deals with setting the look-and-feel of
a Java application. You could list the names of the look-and-feel classes that are installed with the JDK
on your system on the console by adding the following code at the beginning of main() in the example:
UIManager.LookAndFeelInfo[] looks =
for(UIManager.LookAndFeelInfo look : looks) {
For this to work, you need to add the following import statement:
Search WWH ::

Custom Search