Java Reference
In-Depth Information
The
UIManager
class has a
setLookAndFeel(
LookAndFeel
)
method that is used to
choose a program's look and feel. To get a
LookAndFeel
object that you can use with this
method, call one of the following class methods of
UIManager
:
getCrossPlatformLookAndFeelClassName()
—This method returns an object rep-
resenting Java's cross-platform Ocean look and feel.
n
getSystemLookAndFeelClassName()
—This method returns an object representing
your system's look and feel.
n
The
setLookAndFeel()
method throws an
UnsupportedLookAndFeelException
if it
can't set the look and feel.
After you call this method, you must tell every component in an interface to update its
appearance with the new look and feel. Call the
SwingUtilities
class method
updateComponentTreeUI(
Component
)
with the main interface component (such as a
JFrame
object) as the argument.
Under most circumstances, you only should call
setLookAndFeel()
after every compo-
nent has been added to your graphical user interface (in other words, right before you
make the interface visible).
The following statements set up a component to employ the Java look and feel:
try {
UIManager.setLookAndFeel(
UIManager.getCrossPlatformLookAndFeelClassName());
SwingUtilities.updateComponentTreeUI(this);
} catch (Exception e) {
System.out.println(“Can't set look and feel: “”
+ e.getMessage());
e.printStackTrace();
}
The
this
keyword refers to the class that contains these statements. If you used the pre-
ceding code at the end of the constructor method of a
JFrame
, every component on that
frame would be displayed with the Java look and feel.
To select your system's look and feel, use
getSystemLookAndFeelClassName()
, which is
inside the call to
setLookAndFeel()
in the preceding example. This produces different
results on different operating systems. A Windows user would get that platform's look
and feel by using
getSystemLookAndFeelClassName()
. A UNIX user would get the
Motif look and feel, and a Mac OS X user would get the Aqua look and feel.