JaVa
   

Look and Feel

"Look and feel" is a way to make your Java app or applet look like the operating system on which it is being executed, without changing any code. As you can guess, because Java works on most platforms, this is a really useful tool, and it's easy to implement. As we mentioned earlier in this chapter, it is only possible to use look and feel with Swing. The reason we cannot use it with AWT is that AWT uses heavyweight components, whereas Swing components are actually drawn purely in Java. So it is then possible to change the look (and feel) of the components. Let's now look at a simple app, which will look and feel like the operating system on which it is being executed.

Code Listing 8-7: Look and feel example
import javax.swing.*;
public class LookandFeel extends JFrame
{
 public LookandFeel()
 {
 super("Look and Feel Example");
 setSize(400, 300);
 setDefaultCloseOperation(EXIT_ON_CLOSE);
 setVisible(true);
 }
 public static void main(String args[])
 {
 try
 {
 UIManager.setLookAndFeel(UIManager.getSystemLookAnd
 FeelClassName());
 }
 catch (Exception e)
 {
 System.out.println(e);
 }
 LookandFeel theApp = new LookandFeel();
 }
}


Java End example

When we run the example, you will see the following window appear on the screen (see Screenshot-5).

Java Click To expand
Screenshot-5: Java look and feel

This example was run on the Windows platform and therefore uses the Windows-style look and feel. If you compare it to previous windows that we created earlier in this chapter, you can see that the gray color of the windows differs. There are more noticeable differences than this of course, which you will take note of when using different GUI components with different look and feels. We will look at GUI components in more detail in , "Introduction to GUI." To change the look and feel, we can simply call the static setLookAndFeel method of the UIManager class. In our example, we pass in a call to the getSystemLookAndFeelClassName static method of the UIManager class. This can be seen in the following code segment:

try
{
 UIManager.setLookAndFeel(UIManager.getSystemLookAnd
 FeelClassName());
}
catch (Exception e)
{
 System.out.println(e);
}


This checks to see which operating system you are running the app on and then selects the appropriate look and feel for it. Alternatively, we can also specify the look and feel directly, but we are not always guaranteed that it will work, as not all implementations of Java have all the look and feels available. Here is a list of the available look and feels we can use:

Look and Feel Class Name

Description

UIManager.getCrossPlatformLookAndFeelClassName()

This one is always available, as it is the default.

UIManager.getSystemLookAndFeelClassName()

This is the one used in the previous example and will make the app/ applet look and feel like the operating system on which it is running.

"javax.swing.plaf.metal.MetalLookAndFeel"

If available, this will give the app/ applet a metallic look.

"com.sun.java.swing.plaf.windows. WindowsLookAndFeel"

If available, this will make the app/applet look like it is running on the Windows platform.

"com.sun.java.swing.plaf.motif.MotifLookAndFeel"

If available, this will make the app/applet look like CDE/Motif.

"javax.swing.plaf.mac.MacLookAndFeel"

If available, this will make the app/applet look like it is running on the Mac OS.

JaVa
   
Comments