Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swing UI does not have native OS look

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!

like image 620
Virat Kadaru Avatar asked Jun 08 '10 17:06

Virat Kadaru


People also ask

What is pluggable look and feel in Java?

Pluggable look and feel is a mechanism used in the Java Swing widget toolkit allowing to change the look and feel of the graphical user interface at runtime.


1 Answers

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).

like image 128
Durandal Avatar answered Nov 14 '22 21:11

Durandal