3

I use Nimbus Look and Feel in a project. However, although every GUI JComponent have a Look and Feel of Nimbus, JFrame always have Windows Look and Feel.

How can JFrame have Nimbus Look And Feel?

Edit: Operating System : Windows XP

mKorbel
  • 109,525
  • 20
  • 134
  • 319
MOD
  • 1,070
  • 3
  • 19
  • 41
  • 2
    See also [How can I customize the title bar on JFrame?](http://stackoverflow.com/questions/2781987/how-can-i-customize-the-title-bar-on-jframe) – Jonas Sep 30 '11 at 15:27

5 Answers5

8

Try using this:

JFrame.setDefaultLookAndFeelDecorated(true); //before creating JFrames

For more info., see How to Set the Look and Feel in the tutorial.


import javax.swing.*;

class FrameLook {

    public static void showFrame(String plaf) {
        try {
            UIManager.setLookAndFeel(plaf);
        } catch(Exception e) {
            e.printStackTrace();
        }
        JFrame f = new JFrame(plaf);
        f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

        f.setSize(400,100);
        f.setLocationByPlatform(true);
        f.setDefaultLookAndFeelDecorated(true);
        f.setVisible(true);
    }

    public static void main(String[] args) {
        showFrame(UIManager.getSystemLookAndFeelClassName());
        showFrame(UIManager.getCrossPlatformLookAndFeelClassName());
        showFrame("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
    }
}

Frame title bar Look'n'Feel

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Marek Sebera
  • 39,650
  • 37
  • 158
  • 244
  • 3
    @Marek At first I put a comment claiming that the OP must be doing something wrong, but the source I edited into your answer suggests otherwise. Here, while the x-plat frame title-bar is different to the Windows title bar, the Nimbus title bar is identical. I suspect that means that Nimbus makes no special changes to the title-bar. Could users of other OS' confirm my result? – Andrew Thompson Sep 30 '11 at 16:06
  • 1
    That's very wrong: Nimbus doesn't support window decorations, you will always get a system window. – Yago Méndez Vidal Jan 04 '13 at 11:19
4

Confirming @Andrew's suspicion, setDefaultLookAndFeelDecorated() says that, when supported, "newly created JFrames will have their Window decorations provided by the current LookAndFeel." I changed the size to see the whole title.

FrameLook

import javax.swing.*;

class FrameLook {

    public static void showFrame(String plaf) {
        try {
            UIManager.setLookAndFeel(plaf);
        } catch (Exception e) {
            e.printStackTrace(System.out);
        }
        JFrame f = new JFrame(plaf);
        f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

        f.setSize(500, 100);
        f.setLocationByPlatform(true);
        JFrame.setDefaultLookAndFeelDecorated(true);
        f.setVisible(true);
    }

    public static void main(String[] args) {
        showFrame(UIManager.getSystemLookAndFeelClassName());
        showFrame(UIManager.getCrossPlatformLookAndFeelClassName());
        showFrame("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
    }
}
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
2

And confirming based on the Windows Classic UI for XP. enter image description here

Spencer Kormos
  • 8,381
  • 3
  • 28
  • 45
  • Take note that the background color for the pane in Nimbus is different and is the default color, as opposed to the background color of the pane for Windows. – Spencer Kormos Feb 02 '12 at 21:16
1

You can't do it directly since Nimbus doesn't support window decorations, that's why you always get a system window, even with the given answers. Try this very simple code:

import javax.swing.LookAndFeel;
import javax.swing.UIManager;
import javax.swing.UIManager.LookAndFeelInfo;

public class DoesNimbusSupportWindowDecorations {

    @SuppressWarnings("unchecked")
    public static void main(String... args) {
        LookAndFeel nimbus = null;
        for (LookAndFeelInfo lafInfo : UIManager.getInstalledLookAndFeels()) {
            if (lafInfo.getName() == "Nimbus") {
                try {
                    nimbus = ((Class<LookAndFeel>) Class.forName(
                            lafInfo.getClassName())).newInstance();
                } catch (Exception e) {
                    System.err.println("Unexpected exception.");
                }
            }
        }

        if (nimbus != null) {
            System.out.println("Nimbus supports window decorations...? "
                    + (nimbus.getSupportsWindowDecorations() ? "YES" : "NO"));
        } else {
            System.err.println("Your system does not support Nimbus, you can't"
                    + " run this test.");
        }
    }

}

or simply inside your code with the proper import:

System.out.println(new NimbusLookAndFeel().getSupportsWindowDecorations());

What's beyond my understanding is why Sun decided such a thing since the decorations do exist for internal frames and have a custom decoration. I'll be investigating if it's possible to use these decorations by extending NimbusLookAndFeel or playing with defaults, since Nimbus is based on Synth, unsure about the best way.

0

This is how I do. A copy paste from my eclipse project.

import javax.swing.UIManager.LookAndFeelInfo;
import java.awt.EventQueue;
import java.awt.BorderLayout;
import javax.swing.*;
public class Frame1 {
    private JFrame frame;
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {

                     for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
                            if ("Nimbus".equals(info.getName())) {
                                UIManager.setLookAndFeel(info.getClassName());
                                break;
                            }
                        }
                Frame1 window = new Frame1();
                    window.frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }
Tan90
  • 29
  • 4