Changing RichFaces skins in runtime

Setting RichFaces application to use a particular skin is very simple, all you have to do is set skin to use in web.xml:


  org.richfaces.SKIN
  ruby

It is sometimes desirable to change the skins in runtime. Users might even want to save a particular skin in their preferences. Here is how to do it.

First, instead of hard coding a skin name, we are going to set to an EL expression:


  org.richfaces.SKIN
  #{skinBean.skin}

skinBean is a name of JSF manged bean that looks like this:

public class SkinBean {

   private String skin;

   public String getSkin() {
	return skin;
   }
   public void setSkin(String skin) {
	this.skin = skin;
   }
}

Registering in JSF configuration file:


  skinBean
  demo.SkinBean
  session
  
   skin
   ruby
  

We want to initialize the skin property to some initial value. We are also placing the bean in session scope. It wouldn’t make sense to put the bean in request scope as the skin value will be reset on each request. We want the user to select and skin and be applied during the session.

Quickly test how it works, you can use the following page:


   
	
	   
		
		
		
		
		
		
		
		
		
	   
           
		Tabs also change color
		...
	   
        
   

Tab panel is only placed to show how look and feel is updated. Place any other RichFaces UI components on the page to see how look and feel is updated.

Result:

skin-change002.png

skin-change001.png

If you want to learn how to create custom skins, read this blog entry.

One more thing you can try is enabling skinning for standard controls, in our example thaht would be h:selectOneListbox:


   org.richfaces.CONTROL_SKINNING
   enable

Learn more about standard skinning.

5 responses to “Changing RichFaces skins in runtime”

  1. Thanx, good article. I just tried to change the skins dynamically and it worked.

  2. […] RichFaces Using rich:layout and rich:layoutPanel components 4 – part RichFaces webinar series Changing RichFaces skins in runtime Using rich:layout and rich:layoutPanel components RichFaces confirmation dialog – complete […]

  3. Thanks for the post. It was really helpful.

  4. Great post! there is a way to change Locale at runtime the same way?

    1. Yes, there is API to change the Locale in runtime.

Leave a comment