Chapter 22. Containers, Layouts, and AWT Loose Ends

  • Pluggable Look and Feel

  • All About Containers

  • Layout in a Container

  • Tying up the Loose Ends

  • Exercises

  • Some Light Relief—Sky View Cafe: A High Quality Applet

We're now two-thirds of the way through our tour of JFC and Swing. This chapter completes the topic by presenting some containers and an explanation of how you use them to lay out your components neatly on the screen. We also give some information on a couple of topics that are related to the window system in general.

Pluggable Look and Feel

Let's start off the chapter with something that, while not unique to Java, is certainly not widely available. I'm referring to the Pluggable Look and Feel or PLAF as it is usually abbreviated. By default, a Swing program has the Metal look and feel, which is a look and feel designed especially for Java. You can easily change that so a program has the look and feel of Windows, the Macintosh, or of Motif. PLAF means that you, the programmer, can add a few extra lines of code to allow users to select which look and feel they want anytime they run your program, regardless of which operating system is running underneath.

The Pluggable Look and Feel is a win for software portability. Not only do your Java programs run everywhere, but they can even look the same everywhere.

This is a great boon for users who have become comfortable with a program on one particular platform. Now that same knowledge and familiarity can be retained regardless of the execution environment.

As a programmer, you might share my opinion that the look and feel of a window system is not the most important topic in software today. All window systems do roughly the same set of things, and it's no big deal. The fact remains that, for some users, it is a most important topic. Those users buy the software that pays the wages that keep us employed.

Figure 22-1 is an example of the kind of differences you see on different window systems. The three panels show the same controls in three different “look and feel” ways. The top panel is the Metal look and feel. If you choose to have your GUIs display in Metal, your programs will have a consistent look regardless of the operating system or window libraries. The second panel has the Motif look and feel. The lines are so delicate and thin that some of them don't reproduce completely on this screen capture. The bottom panel is the basic Windows look and feel. Pay no attention to the relative sizes of fonts and buttons. That can all be resized to suit the program. The difference is in how much shading appears around a button, how thick the dot is in a selected radio button, whether components are given a 3-D look, and so on.

Figure 22-1. A Pluggable Look and Feel (PLAF)

image

Here's the really cool thing: there is only one program here! And they are all running on a Windows PC. When you select one of those JRadioButtons, it transforms the program to display in that style. I started up three copies of the program, chose a different look and feel in each, put them next to each other, and took a screen shot. This is less than 150 lines long, and the key area is a couple of lines. It declares the JComponents and a listener for the radio buttons.

The listener has the code (shown here) that does the magic:

String metalPLAFName = "javax.swing.plaf.metal.MetalLookAndFeel";
String motifPLAFName = "com.sun.java.swing.plaf.motif.MotifLookAndFeel";
String winPLAFName = "com.sun.java.swing.plaf.windows.WindowsLookAndFeel";
String lnfName = e.getActionCommand();
try {
     UIManager.setLookAndFeel(lnfName);
     SwingUtilities.updateComponentTreeUI(frame);
     frame.pack();
} catch (Exception exc) { ...

There are a couple of methods in the library to let you force the look and feel to match the system on which you are running. The following code does that:

UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());

The following code forces a program to use the common Metal look:

UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());

Since the look and feel is rendered by Java code, not by use of native libraries, it is feasible to have any look and feel on any platform. Microsoft and Apple, however, have chosen not to grant permission for their look and feel to be used on other platforms.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.16.217.187