Swing UI does not have native OS look
Asked Answered
S

1

5

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!

Stacy answered 8/6, 2010 at 17:7 Comment(5)
What version of Java are you using?Weiser
What you have posted should work, Have you looked at java.sun.com/docs/books/tutorial/uiswing/lookandfeel/… ?Bubb
@Romain - That is where I got the code from and it worked before. Now surprisingly its giving a different look. I also tested it on different systems with the same result.Stacy
Is it failing and printing a stack trace? If you remove the try-catch, you might see an error that would illuminate the issue.Ulda
@Virat Kadaru I am assuming that you must be overriding the settings elsewhere. It is the only thing that makes sense. Do you have anything on the command line ? How about doing a search on UIManager to see if you modify it elsewhereBubb
C
8

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

Chockablock answered 8/6, 2010 at 18:45 Comment(1)
Thanks! That was it. You save me a lot of pain.Stacy

© 2022 - 2024 — McMap. All rights reserved.