1

I want get this appearance for a JButton:

lookandfeel1

I have tried to change the Look and Feel to Windows this way:

  UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");

but it is shown as:

lookandfeel2

Thanks in advance!

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Héctor
  • 24,444
  • 35
  • 132
  • 243
  • 1
    Just a stupid question... are you running the program on windows? (Also, if you try to change it on run time, you'd need to call `SwingUtilities.updateComponentTreeUI()`). – kiheru Dec 15 '14 at 14:41
  • Yes, I'm on Windows. That is the first line in main function. – Héctor Dec 15 '14 at 14:42
  • 4
    OK. No good idea then. (Though I'd use `UIManager.getSystemLookAndFeelClassName()` rather than a hard coded name for portability). – kiheru Dec 15 '14 at 14:45
  • 1
    Use `UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());` Or rather, what @kiheru beat me to - +1. – Andrew Thompson Dec 15 '14 at 14:45
  • 1
    The outcome looks like Windows Classic instead of Windows L&F: `com.sun.java.swing.plaf.windows.WindowsClassicLookAndFeel`. Agree with `getSystemLookAndFeelClassName()`. – dic19 Dec 15 '14 at 14:46
  • I have changed to `getSystemLookAndFeelClassName()` but it remains with the same appearance. – Héctor Dec 15 '14 at 14:50

1 Answers1

2

Taking the comment train to a standalone answer:

    // sets the look and feel to be that of the operating system's
    try {
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
    } catch (ClassNotFoundException | 
        InstantiationException | 
        IllegalAccessException | 
        UnsupportedLookAndFeelException e) {
        e.printStackTrace();
    }

I upvoted kiheru, but it seems like you're not setting it at the right time. You need to run this code before the components are displayed - I usually insert this into the original run() method that starts my application, though it varies if I include a splashscreen.

Also, in future (especially when talking about GUI code, which can get horrendous) people prefer it if you post runnable code examples, so we can see exactly where you might be getting things wrong :)

Gorbles
  • 1,169
  • 12
  • 27
  • 1
    ***"You need to run this code before the components are displayed"*** Either that or update the component tree as seen in [this answer](http://stackoverflow.com/a/5630271/418556). – Andrew Thompson Dec 17 '14 at 08:56
  • That's a nifty note; I'm not sure when people would want to use this (compatibility mode design? Perhaps), but that's something new to bookmark! – Gorbles Dec 17 '14 at 14:08