Richfaces calendar example

Important notice: Richfaces framework reached End of Life in 2016. Therefore, you cannot expect fixes or enhancements unless you fork the project and upgrade yourself the framework.
We recommend reading these guidelines, if you want to modernize your Richfaces application: How to migrate Richfaces Web applications to another Web UI

The  <rich:calendar>   component is used to create inputs for date and time and enter them inline or using pup-up calendar that allows to navigate through months and years.

The RichFaces Calendar has over 80 available attributes but you can generally make do with fewer.

Here’s a basic sample of RichFaces Calendar:

 <rich:calendar value="#{calendarBean.selectedDate}"
                        locale="#{calendarBean.locale}"
                        popup="#{calendarBean.popup}"
                        datePattern="#{calendarBean.pattern}"                      
                        cellWidth="24px" cellHeight="22px" style="width:200px"/>

This is the corresponding CalendarBean:

package sample;

import java.text.DateFormat;
import java.util.Date;
import java.util.Locale;

import javax.faces.event.ValueChangeEvent;

public class CalendarBean {

    private static final String[] WEEK_DAY_LABELS = new String[] { "Sun *",
            "Mon +", "Tue +", "Wed +", "Thu +", "Fri +", "Sat *" };
    private Locale locale;

    private boolean popup;
    private boolean readonly;
    private boolean showInput;
    private boolean enableManualInput;    
    private String pattern;
    private Date currentDate;
    private Date selectedDate;
    private String jointPoint;
    private String direction;
    private String boundary;

    private boolean useCustomDayLabels;

    public Locale getLocale() {
        return locale;
    }

    public void setLocale(Locale locale) {
        this.locale = locale;
    }

    public boolean isPopup() {
        return popup;
    }

    public void setPopup(boolean popup) {
        this.popup = popup;
    }

    public String getPattern() {
        return pattern;
    }

    public void setPattern(String pattern) {
        this.pattern = pattern;
    }

    public CalendarBean() {

        locale = Locale.US;
        popup = true;
        pattern = "MMM d, yyyy";
        jointPoint = "bottomleft";
        direction = "bottomright";
        readonly = true;
        enableManualInput=false;
        showInput=true;
        boundary = "inactive";
    }
    
    
    public boolean isShowInput() {
        return showInput;
    }

    public void setShowInput(boolean showInput) {
        this.showInput = showInput;
    }

    public boolean isEnableManualInput() {
        return enableManualInput;
    }

    public void setEnableManualInput(boolean enableManualInput) {
        this.enableManualInput = enableManualInput;
    }

    public boolean isReadonly() {
        return readonly;
    }

    public void setReadonly(boolean readonly) {
        this.readonly = readonly;
    }

    public void selectLocale(ValueChangeEvent event) {

        String tLocale = (String) event.getNewValue();
        if (tLocale != null) {
            String lang = tLocale.substring(0, 2);
            String country = tLocale.substring(3);
            locale = new Locale(lang, country, "");
        }
    }

    public boolean isUseCustomDayLabels() {
        return useCustomDayLabels;
    }

    public void setUseCustomDayLabels(boolean useCustomDayLabels) {
        this.useCustomDayLabels = useCustomDayLabels;
    }

    public Object getWeekDayLabelsShort() {
        if (isUseCustomDayLabels()) {
            return WEEK_DAY_LABELS;
        } else {
            return null;
        }
    }

    public String getCurrentDateAsText() {
        Date currentDate = getCurrentDate();
        if (currentDate != null) {
            return DateFormat.getDateInstance(DateFormat.FULL).format(
                    currentDate);
        }

        return null;
    }

    public Date getCurrentDate() {
        return currentDate;
    }

    public void setCurrentDate(Date currentDate) {
        this.currentDate = currentDate;
    }

    public Date getSelectedDate() {
        return selectedDate;
    }

    public void setSelectedDate(Date selectedDate) {
        this.selectedDate = selectedDate;
    }

    public String getJointPoint() {
        return jointPoint;
    }

    public void setJointPoint(String jointPoint) {
        this.jointPoint = jointPoint;
    }

    public void selectJointPoint(ValueChangeEvent event) {
        jointPoint = (String) event.getNewValue();
    }

    public String getDirection() {
        return direction;
    }

    public void setDirection(String direction) {
        this.direction = direction;
    }

    public void selectDirection(ValueChangeEvent event) {
        direction = (String) event.getNewValue();
    }

    public String getBoundary() {
        return boundary;
    }

    public void setBoundary(String boundary) {
        this.boundary = boundary;
    }

}

This would produce a RichFaces Calendar using US Locale, arranged with the format “MMM d, yyyy”.

Here’s a preview of what you’ll get:

richfaces calendar

References: http://livedemo.exadel.com/richfaces-demo/richfaces/calendar.jsf?c=calendar&tab=usage

Found the article helpful? if so please follow us on Socials