Skip to main content

Nimbus Look and Feel frame decorations - 1st step: something functional

Posted by Anthra on February 26, 2014 at 4:21 AM PST

Nimbus is a cross-platform look and feel introduced in the Java SE 6 Update 10 (6u10) release. It's not a perfect look and feel for several reasons but it has great potential. One of these reasons is the lack of window decorations support (Bug JDK-6675399).
You can check the window decorations support of a look and feel by simply call the method getSupportsWindowDecorations on it.

NimbusLookAndFeel nlaf = new NimbusLookAndFeel();
System.out.println(""+nlaf.getSupportsWindowDecorations());
Like indicated in the comments of the method if it returns true that means that the look and feel returns instances of RootPaneUI to provide Window decorations to JRootPane.

The goal of this article series is to show you how to build your own frame decorations for nimbus. The goal is not to provide a fully-tested ready to use code (don't expect it to be perfect).

First step rendering

So for now the two things required to have our look and feel supporting window decorations are:

  • Return true to the getSupportsWindowDecorations method
  • Provide a class extending RootPaneUI and register it in the look and feel
For the first point we will simply extend the Nimbus look and feel, let's call it AdvancedNimbus.
public class AdvancedNimbusLookAndFeel extends NimbusLookAndFeel{
       
    @Override
    public boolean getSupportsWindowDecorations() {
        return true;
    }

}

You will have to register that new look and feel in the UIManager.
UIManager.installLookAndFeel("AdvancedNimbus", AdvancedNimbusLookAndFeel.class.getName());



For the second point we will use an already defined RootPaneUI. The one of Metal look and feel will do the job. In this example we will set the default RootPaneUI class just after choosing our look and feel. Later we will define it inside our look and feel class.
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
    if ("AdvancedNimbus".equals(info.getName())) {
        UIManager.setLookAndFeel(info.getClassName());
        UIManager.getLookAndFeel().getDefaults().put("RootPaneUI", MetalRootPaneUI.class.getName());
        break;
    }
}

The last thing to do is to tell to JFrames to use frame decorations.
JFrame.setDefaultLookAndFeelDecorated(true);

That's all create a new JFrame and display it. The result is ugly but our first goal is achieved.

In the next step of this adventure we will see how to use nimbus icons ;).

AttachmentSize
FirstStep.png958 bytes
NimbusFrameDecorationStep1NetBeansProject.zip27.48 KB
Related Topics >>