As you don't know, I've been learning Java, and I have just started out studying for a SCJP (Sun Certified Java Programmer) qualification. While waiting for the course to arrive (you've got to love snail mail), I've been tinkering with Netbeans and building GUIs.
Being a cross-penguin kind of guy, I've noticed that applications that don't use the "Metal" look and feel tend to get some ugly distortions on some operating systems, as such, it can sometimes be helpful to over-ride the system default look and feel, and force your own. Here's how to do it.
First, you'll need the following two imports, if you don't have them already:
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
The function that I use is below, but ultimately you only need the following code to change the Look and Feel:
UIManager.setLookAndFeel(lafString);
where lafString is a string containing one of the following lines:
// For the Windows LaF:
lafString = "com.sun.java.swing.plaf.windows.WindowsLookAndFeel";
// For the GTK LaF
lafString = "com.sun.java.swing.plaf.gtk.GTKLookAndFeel"
// For the Motif (CDE) LaF
lafString = "com.sun.java.swing.plaf.motif.MotifLookAndFeel"
// For the Metal (default Java) LaF
lafString = "com.sun.java.swing.plaf.metal.MetalLookAndFeel"
You must call the setLookAndFeel() method before your GUI is displayed, otherwise you will get unexpected results.
Below is a function that I use to automatically set the Look and Feel based on the host operating system as on Linux systems, I've found it keeps using Metal instead of the GTK theme.
public static void initLaF()
{
final String lafWindows = "com.sun.java.swing.plaf.windows.WindowsLookAndFeel";
final String lafGTK = "com.sun.java.swing.plaf.gtk.GTKLookAndFeel";
final String lafMetal = "com.sun.java.swing.plaf.metal.MetalLookAndFeel";
final String lafMotif = "com.sun.java.swing.plaf.motif.MotifLookAndFeel";
try {
String operatingSystem;
operatingSystem = System.getProperty("os.name");
if (operatingSystem.equalsIgnoreCase("linux"))
{
UIManager.setLookAndFeel(lafGTK);
} else if (operatingSystem.startsWith("Windows")) {
UIManager.setLookAndFeel(lafWindows);
} else if (operatingSystem.startsWith("solaris")) {
UIManager.setLookAndFeel(lafMotif);
} else {
UIManager.setLookAndFeel(lafMetal);
}
} catch (UnsupportedLookAndFeelException e) {
// Add exception code here
} catch (ClassNotFoundException e) {
} catch (InstantiationException e) {
} catch (IllegalAccessException e) {
}
}
And that's it, just call initLaF() before your GUI starts, and you're all set.