Search code examples
javajsf-2primefacesglassfish-3

Update tab title in accordion


I have a PrimeFaces 3.5 accordionPanel like this one:

<p:accordionPanel cache="false">
    <p:ajax event="tabChange" listener="#{pPRBean.onChangeTab}" />
    <p:tab title="Item">
        <h:outputText value="#{pPRBean.counter}"/>
    </p:tab>
    <p:tab title="Item">
        <h:outputText value="#{pPRBean.counter}"/>
    </p:tab>
    <p:tab title="Item">
        <h:outputText value="#{pPRBean.counter}"/>
    </p:tab>
    <p:tab title="Item">
        <h:outputText value="#{pPRBean.counter}"/>
    </p:tab>
</p:accordionPanel>

And a backing bean like this one:

package com.gecolsa.test.view;

import javax.ejb.Stateless;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import org.primefaces.component.tabview.Tab;
import org.primefaces.event.TabChangeEvent;

@Stateless
@ManagedBean
@RequestScoped
public class PPRBean {

    private int counter = 0;
    private String firstName;

    public String getFirstName(){
        return firstName;
    }

    public void setFirstName(String firstName){
        this.firstName = firstName;
    }

    public int getCounter() {
        return counter;
    }

    public void setCounter(int counter) {
        this.counter = counter;
    }

    public void onChangeTab(TabChangeEvent event){
        Tab activeTab = event.getTab();
        if(!activeTab.getTitle().endsWith("[Pressed]")){
            activeTab.setTitle(activeTab.getTitle() + " [Pressed]");            
        }
        System.out.println(activeTab.getTitle());
        counter = counter + 1;
    }
}

My question is: How can I change the title with the new value of the counter? It only shows "Item" although the tab returns a new value...(I wanna update the title of the tab in runtime wherever a user clicks on it, with additional information, like hour and user who clicked). I'm using glassfish 3.1.2.


Solution

  • You can update the tab by updating the accordionPanel from the listener like below:

    public void onChangeTab(TabChangeEvent event){
        Tab activeTab = event.getTab();
        if(!activeTab.getTitle().endsWith("[Pressed]")){
            activeTab.setTitle(activeTab.getTitle() + " [Pressed]");            
        }
        System.out.println(activeTab.getTitle());
        counter = counter + 1;
        RequestContext.getCurrentInstance().update("panel");
    }
    

    where panel is the id of <p:accordionPanel id="panel">

    Since tab has no renderer you have to update the whole panel instead of just the tab.