Posted on Aug 20, 2008

Skinning a Slider with Nimbus

So time for a example of how to skin a Swing JSlider using the Nimbus Look and Feel and some simple painting code. So this is what we get with the standard Nimbus look slider on a dark grey background.

Slider Default Look

All of the Nimbus skin comes from a set of properties in the UIManager defaults table. The keys we will be changing for this example are:

  • “Slider.thumbWidth”
  • “Slider.thumbHeight”
  • “Slider:SliderThumb.backgroundPainter”
  • “Slider:SliderTrack.backgroundPainter”

You can customize the look for a Component either globally for all instances of the component or locally for a single component instance. To change all sliders globally you can set these properties using UIManager.put(key,value) but in this example I will just set them locally for a single slider. To set UI defaults localy for a single component instance you need to create a UIDefaults map, insert you properties and then set it as a client property on the component:

                JSlider slider = new JSlider(0, 100, 50);
                UIDefaults sliderDefaults = new UIDefaults();
                ....
                sliderDefaults.put(<<key>>,<<value>>)
                ....
                slider.putClientProperty("Nimbus.Overrides",sliderDefaults);
                slider.putClientProperty("Nimbus.Overrides.InheritDefaults",false);

The “Nimbus.Overrides.InheritDefaults” key states if the values in “Nimbus.Overrides” should be merged with the defaults(false) or replace them(true). So next some examples for what properties we should set to skin the slider:

                sliderDefaults.put("Slider.thumbWidth", 20);
                sliderDefaults.put("Slider.thumbHeight", 20);
                sliderDefaults.put("Slider:SliderThumb.backgroundPainter", new Painter() {
                    public void paint(Graphics2D g, JComponent c, int w, int h) {
                        g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
                        g.setStroke(new BasicStroke(2f));
                        g.setColor(Color.RED);
                        g.fillOval(1, 1, w-3, h-3);
                        g.setColor(Color.WHITE);
                        g.drawOval(1, 1, w-3, h-3);
                    }
                });
                sliderDefaults.put("Slider:SliderTrack.backgroundPainter", new Painter() {
                    public void paint(Graphics2D g, JComponent c, int w, int h) {
                        g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
                        g.setStroke(new BasicStroke(2f));
                        g.setColor(Color.GRAY);
                        g.fillRoundRect(0, 6, w-1, 8, 8, 8);
                        g.setColor(Color.WHITE);
                        g.drawRoundRect(0, 6, w-1, 8, 8, 8);
                    }
                });	

The lets pop that code into a sample app and run it and see what it looks like.

Slider Demo

The original Nimbus slider on the bottom and the skinned one on top, both running in the same application. Here is the source so you can try it your self. Let me know if this was useful and what other examples you would like to see.

Java Icon
SliderSkinDemo.java

Posted on Aug 20, 2008

A very late “Nimbus is Done” blog!

Wow its been crazy, we have finally finished Nimbus for 6u10 and release candidate is out there Java 6u10 RC Download, have done Java One and I am now knee deep in Java FX. I have about 10 half completed blogs and just never seem to find the time to finish them. As well as being mega busy with Nimbus and Java FX I have just moved back from California to the UK. Moving is always way too time consuming and expensive.

If you missed our talk and BOF at Java One this year here are the presentations:

Java One 2008 Nimbus Talk PDF

Java One 2008 Nimbus BOF Presentation PDF

A lot of you have been asking about the Nimbus Designer Tool that I demoed at Java One. I have been working at getting it opensourced but it is a slow bureaucratic process so I am sorry. In the mean time I thought I would try and do a couple blogs on how you can skin Nimbus your self with out the tool. It is very easy to do providing you are not worried about getting your hands dirty writing some Java 2D drawing code. You could always use images or a SVG library if you don’t fancy raw Java 2D. I would like to split the designer tool into 2 parts the first part the lets you create a Painter classes with Java 2D drawing code by drawing graphics. The second part would let you assign painters to components and generate the code to populate the UIManager defaults table. But both those parts are not hard to do your self if we help you get started so I will see what I can do.

So just a quick hello to say I am still alive, next to try and post a couple blogs on how to skin Nimbus.

SwingSet 3 with Nimbus

Posted on Dec 7, 2007

Nimbus: Large, Small, Mini Components

In the original design for Nimbus there were large,small and mini versions of components. Small and mini components are very useful when you are creating tool pallets or other UI where space is very tight. Apple recently added this to their new look and feel in the latest java versions see Technical Note TN2196. We are using the same client property key and values as Apple to be compatible but I have also added “large” as a option. Not sure “large” is as useful as “small” or “mini” but might look good in wizards or dialogs. Check out the screenshot to see how they look. Nimbus Small Mini 600

Posted on Oct 30, 2007

JavaOne 2008 Papers

Its already that time of year again to start writing up all your great ideas for Java One Desktop talks for next year. Its only two and a half weeks till they close for submissions so hurry up. Click here for details for paper submission.

I am planning on submitting a talk on Nimbus and the new Nimbus Designer tool. Covering:

  • Using Nimbus L&F in your applications
  • Designing for cross-platform, how to design your application to look good on all platforms
  • Customizing Nimbus
  • Creating new look and feels using the designer tool based on Nimbus
  • Creating your own components with Nimbus L&F themeing support
  • Designing the look for your own components using the deisgner tool

Might have too much to cover in a hour, don’t want to scare you all off. Any thoughts on what topics you think are the most important/intresting in case I need to cut bits out or anything I didn’t mention here?

JavaOne 2008

Posted on Oct 1, 2007

Nimbus Early Access

Finally a version you can all go and play with. Nimbus is coming out as part of “Java SE 6 update N” formerly known as the “Consumer Release” and “Project Hamburg”. Well whatever it is called it contains a decent version of the new Nimbus look and feel. You can go and download it from Java SE 6 Update N Early Access Program. Its is definately not 100% finished yet, there are a few areas that we are still working on:

  • Tabs
  • Focus handling is missing from Spinner,Slider, Tree, Table and List
  • FileChooser has a lot of issues
  • Right to Left for international language support
  • 3rd party component theming
  • Color theming
  • Lots of minor things like table gridlines or setContentAreaFilled() support on buttons
  • Exceptions are thrown if the window or some components get too small

Most of these will be fixed in the next EA release or the first beta release. A few of them have been fixed in the last couple weeks, I have just been working on color theming. All colors in Nimbus are derived from a set of UIDefault colors with Hue, Saturation and Brightness offsets. This gives you reasonable control over changing the colors of the Nimbus Look and Feel.

Dark NimbusDarker Themed Nimbus

Using Nimbus

There are 3 easy ways to use the Nimbus look and feel:

  1. If you query UIManager for a list of available look and feels then Nimbus will show up in the list
  2. Add UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel"); to your application before creating any Swing components
  3. To specify the L&F from command line, use the following when running your Java application: -Dswing.defaultlaf=com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel

The reason Nimbus is in the sun.swing… package is it is not possible to add new API to the javax.swing… package in a update release. We hope that we can move Nimbus to javax.swing in Java 7 and if it turns out to be popular then we push to make it the default look and feel in 7. Before 6 update N is final we will work out some way for you to be able to use it now and when it is potentially moved without changing any code.

If you Find Bugs

You can search to see if there is an existing at Sun Bug Database or you can view a List of open Nimbus Bugs. You are welcome to post bugs here as comments and I will file them if they are not known bugs.

Other Cool Features of Update N

Improved performance

  • The Quick Starter feature will prefetch portions of the JRE into memory, substantially decreasing the average JRE cold start-up time (the time that it takes to launch a Java application for the first time after a fresh reboot of a PC).
  • Hardware acceleration support: Java SE 6 Update N introduces a fully hardware accelerated graphics pipeline based on the Microsoft Direct3D 9 API, translating into improved rendering of Swing applications which rely on translucency, gradients, arbitrary transformations, and other more advanced 2D operations.

Enhanced JRE installation experience 

  • The Deployment Toolkit takes the guess work out of determining what versions of the JRE end users have installed on their PC. It supplies Java based web applet/application deployers with a simple interface to accomplish Java detection and installation.
  • The Kernel installation mode lets first time Java users run applets and Web Start applications without waiting for the whole JRE download. While the default Kernel installation will work with existing Java applets, application developers have the ability to select libraries that should be installed with the kernel, before the rest of the JRE is installed on the end user’s system.
  • For current users of Java SE, the JRE update mechanism has also been improved, using a patch-in-place mechanism that translates in a faster and more reliable update process (the patch in place mechanism will take effect for end users who upgrade from this update release or later to a new update release). As an added benefit, follow-on update releases will no longer be listed as separate items in the Windows “Add or Remove Programs” dialog.
  • A new Java update download engine provides end users with the convenience of pausing and resuming the JRE download, and relies on a more reliable download mechanism.