Swing UI does not have native OS look
-
24-10-2019 - |
Question
I am building an application in java swing and I am using the following code to give the UI a native OS look
try {
UIManager.setLookAndFeel(
UIManager.getSystemLookAndFeelClassName());
} catch (Exception e) {
e.printStackTrace();
}
On a OS X, the look is fine, but on windows (XP and 7) the buttons look like this.
alt text http://img710.imageshack.us/img710/8735/buttonsoc.png
I have used this exact same code on other projects and it works fine. But in this particular project I get a completely different look.
I am using Java 1.6
Thanks in advance!
Solution
Are you possibly creating your GUI elements before actually setting the L&F? If you already created (e.g.) JButton instances and called methods on them, they allocate their UI peer - changes to the L&F after that won't affect the already created instances.
This would explain why it works on Mac (the L&F defaults to Mac on Apple's JVM IIRC), but not on Windows. You can test this quickly if you move setting the L&F directly into your main method as the very first call (this assuming your main class does NOT contain any statically initialized GUI instances of course).