Search code examples
javaswingjbuttonlook-and-feel

JButton Windows Look and Feel


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!


Solution

  • 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 :)