GUI Look & Feel
Look And Feel is very important, it defines how components look and act (feel) . For instance, what you've been seeing when you have been putting components on a JPanel is the Java Look And Feel. The JButtons don't look normal, do they? Nope. Just like how Windows program looks different from a Java or Mac program, that's look and feel. A Java program typically can have one of the 5 or so possible LAFs(Look And Feels) :
- Use the current OS's Look and Feel.
- Use the Java (a.k.a Metal) Look and Feel.
- Use the Motif (UNIX) Look and Feel.
- Use the Windows LAF (only on windows).
- Use the Mac LAF (only on mac).
To set the look and feel BEFORE constructing any components, use the UIManager class. It contains the setLookAndFeel(String classname) method that (as shown) takes a string that is the class name of the LAF that you want to use for your program.
Here's the code to use the Motif LAF:
Try {
UIManager.setLookAndFeel("com.sun.java.swing.motif.MotifLookAndFeel");
}
catch(ClassNotFoundException e) {}
Here's the code to use the current OS's LAF:
Try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}
catch(ClassNotFoundException e) {}
The Windows LAF Class Name is com.sun.java.swing.windows.WindowsLookAndFeel
The Mac LAF Class Name is com.sun.java.swing.mac.MacLookAndFeel
The Java LAF Class Name is com.sun.java.swing.plaf.metal.MetalLookAndFeel
To use the Java LAF (even though it IS the default) use the method UIManager.getCrossPlatformLookAndFeelClassName()
That's it for Look and Feel. If you manage to find other LAFs on the web. They are used in the same way, just find out its class name!