 | Level: Introductory John Zukowski (jaz@zukowski.net), President, JZ Ventures, Inc.
27 May 2003 In an attempt to keep up with the latest and greatest in user desktops, Sun has updated the standard set of look-and-feel designs provided with the J2SE 1.4 platform. In this installment of Magic with Merlin, John Zukowski uses the latest 1.4.2 release (in beta at the time of this writing) to show Windows users how to get the newer look of Windows XP, as well as the GTK+ look typical of a Linux desktop.
While all of the Magic with Merlin columns have so far been about features new to the Java 1.4 release, this latest column is specific to the 1.4.2 version, now in beta. Sun has decided that the Windows classic look has grown old and nobody uses Motif anymore -- at least on the typical user desktop. What you'll find in this latest version of the standard Java platform is an updated Windows look and feel that looks like Windows XP when the user is on a Windows XP box, and a completely new look and feel called GTK+, which has the look and feel of a common Linux desktop.
The Swing architecture provides a pluggable look-and-feel framework. The framework offers a standard way, for instance, to make the font of all components 8-point bold Lucida, without having to change every call to create a component. You just plug in a new setting in the look and feel and magically everything picks up the changes. You can even change the overall style of the GUIs by setting the look and feel. Previously, J2SE 1.4 offered look-and-feel designs for platform-specific UIs like Microsoft Windows and Motif. It also provided a cross-platform UI called Metal that was specific to the Java platform, but looked the same no matter which desktop a user was on.
Changing look-and-feel designs
Before we examine the latest look-and-feel designs, let's demonstrate how to change the look and feel of a Java program, as shown in Listing 1:
Listing 1. Looking up all display modes
import java.awt.*;
import javax.swing.*;
public class Start extends JFrame {
public Start() {
setDefaultCloseOperation(EXIT_ON_CLOSE);
Container c = getContentPane();
JButton button = new JButton("Hello");
c.add(button, BorderLayout.CENTER);
setSize(100, 50);
show();
}
public static void main(String[] args) {
try {
UIManager.setLookAndFeel(
UIManager.getCrossPlatformLookAndFeelClassName());
//UIManager.getSystemLookAndFeelClassName());
} catch (Exception e) {
}
new Start();
}
}
|
The setLookAndFeel() call of UIManager allows you to pass in the fully qualified class name of the new look-and-feel design. Instead of worrying about what the class name is for the cross-platform Metal UI (javax.swing.plaf.metal.MetalLookAndFeel), you can just ask the system for the name with the getCrossPlatformLookAndFeelClassName() method. On the other hand, getSystemLookAndFeelClassName() will return the UI class name specific for the user's desktop. If the user is running the program on a Windows box, he'll get the Windows UI through the com.sun.java.swing.plaf.windows.WindowsLookAndFeel class. On UNIX boxes, the Motif/CDE interface or the com.sun.java.swing.plaf.motif.MotifLookAndFeel class is returned. The new GTK+ look and feel is available with J2SE 1.4.2, but is not returned as the system look-and-feel class for UNIX machines. This situation will supposedly change with the J2SE 1.5 release. Figure 1 shows what the Start program output looks like when run on a Windows 2000 box:
Figure 1. Cross Platform UI
Windows XP and UNIX users will see a slightly different look for the system, as shown in Figure 2:
Figure 2. Windows Classic UI
Note: Macintosh users of Apple's Java version will have an Aqua look and feel available.
As I indicated, Windows 2000 users will see one UI, and Windows XP users will see a different one. The new Windows XP experience is not a full-fledged set of look-and-feel classes. Instead, the existing set of Windows UI classes will display the classic version of Microsoft Windows for non-XP users and the Windows XP version for Windows XP users. Setting the system property swing.noxp, as shown below, provides a way for Windows XP users to get the more classic Windows interface. There is no option for a non-XP Windows user to get the newer XP interface.
java -Dswing.noxp=true Start
|
The GTK+ UI
While the new Windows XP experience is just a modified Windows look and feel, the GTK+ UI is brand new and based on GTK+ 2.0 (see Resources for a link to information about GTK+ 2.0). Non-Linux users will probably find this name new to them. Think of it as a typical Linux desktop UI experience (though certainly not the only one available). Many parts of GTK+ are customizable by the user, and by placing configuration files in special locations, the look and feel
of the desktop changes accordingly. In addition, capabilities like the multi-document interface (MDI) -- called JInternalFrame in Swing -- are not part of the framework but are left to the underlying window manager. Thus, desktops can be customized by the theme engine. With the Swing UI for GTK+, theming is supported by a project named Metacity (see Resources). By setting the system property, swing.metacitythemename, you can control which GTK+ theme is used. The Crux and Bluecurve themes, in addition to the default theme, are supported. You can then place additional themes in directories specified by the swing.gtkthemedir system property. GTK+ resource files also provide a means to customize the look and feel. Currently, there is no support for creating additional
GTK engines. However, you can visit Themes.org to see what may be in store.
 |
SwingSet2 and the new UIs
The easiest way to demonstrate the new look-and-feel designs is by illustrating them with the SwingSet2 demonstration program that comes standard with J2SE, which you'll find in the demo\jfc\SwingSet2 directory wherever you installed J2SE 1.4. Just start up the program with the command java -jar SwingSet2.jar, which uses the -jar option to run the main class specified in the manifest file for the JAR.
The initial screen displays some JInternalFrame components along with several buttons -- items you might find on a typical desktop application. Figure 3 shows what the SwingSet app looks like on a Windows XP machine:
Figure 3. SwingSet with Windows XP look and feel
Figure 4 shows the GTK+ look and feel:
Figure 4. SwingSet with GTK+ look and feel
One of the more noticeable Windows changes is to the file dialog. Figure 5 shows what the Windows XP file dialog looks like:
Figure 5. Windows XP file dialog
On the GTK+ side, Figure 6 shows a more user-friendly look to its popups:
Figure 6. GTK+ warning dialog
Summary
J2SE 1.4.2 highlights Sun's commitment to keeping up with the latest standards on the Java desktop. With the Windows XP and GTK+ look-and-feel designs, users will see Java applications that look more like native applications. Besides the new look-and-feel designs, J2SE 1.4.2 includes lots of bug fixes and a JFileChooser that, under certain circumstances, is actually 300 percent faster. (You've got to wonder what took so long if it was that slow, but at least it's faster now.)
Resources
About the author
Rate this page
|  |