Saturday, 14 July 2012

Apache Click: RightCheckBox Control a Custom Checkbox Class

Click API Checkbox Customization

This customization is required to provide the labels to the right. Current available API is designed in this package which is providing label to the left only and if we want to use the checkbox with label on right then we have to override the current class and make the changes into this. Current available checkbox is available in the following options only:
Let's have a look on how we can customize the existing feature of the apache click control class.

import java.text.MessageFormat;

public class RightCheckBox extends Checkbox {

    private static final long serialVersionUID = 1L;

    // -------------------------------------------------------------- Constants
    private String rightLabel;
    protected final static String VALIDATE_CHECKBOX_FUNCTION =
        "function validate_{0}() '{'\n"
        + "   var msg = validateCheckbox(''{0}'',{1}, [''{2}'']);\n"
        + "   if (msg) '{'\n"
        + "      return msg + ''|{0}'';\n"
        + "   '}' else '{'\n"
        + "      return null;\n"
        + "   '}'\n"
        + "'}'\n";

    /** The field checked value. */
    protected boolean checked;

    // ----------------------------------------------------------- Constructors

    public RightCheckBox(String name) {

    public RightCheckBox(String name, String label) {
        super(name, label);

    public RightCheckBox(String name, boolean required) {

    public RightCheckBox() {

    // ------------------------------------------------------ Public Attributes
    public String getTag() {
        return "input";

    public boolean isChecked() {
        return checked;

    public void setChecked(boolean value) {
        checked = value;

    public String getType() {
        return "checkbox";

    public String getValue() {
        return String.valueOf(checked);

    public void setValue(String value) {
        checked = Boolean.valueOf(value);

   public void setRightLabel(String label){
    public String getRightLabel(){
      return rightLabel;
    public Object getValueObject() {
        if (checked) {
            return Boolean.TRUE;

        } else {
            return Boolean.FALSE;

    public void setValueObject(Object object) {
        if (object != null && object instanceof Boolean) {
            checked = (Boolean) object;

    public String getValidationJavaScript() {
        if (isRequired()) {
            Object[] args = new Object[3];
            args[0] = getId();
            args[1] = String.valueOf(isRequired());
            args[2] = getMessage("not-checked-error", getErrorLabel());

            return MessageFormat.format(VALIDATE_CHECKBOX_FUNCTION, args);

        } else {
            return null;

    // --------------------------------------------------------- Public Methods
    public void bindRequestValue() {
        setChecked(getContext().getRequestParameter(getName()) != null);

    public boolean onProcess() {
        if (isDisabled()) {
            Context context = getContext();

            // Switch off disabled property if control has incoming request
            // parameter. Normally this means the field was enabled via JS
            if (context.hasRequestParameter(getName())) {
            } else {
                // If field is disabled skip process event
                return true;

        // In Html an unchecked Checkbox does not submit it's name/value so we
        // always validate and dispatch registered events

        if (getValidate()) {


        return true;
    public void render(HtmlStringBuffer buffer) {

"); } @Override public void validate() { setError(null); if (isRequired() && !isChecked()) { setErrorMessage("not-checked-error"); } } }
After customization and overriding the methods the new class will have the backward compatibility and we have the option to use both either right or left label using RightCheckBox class. After overrriding the current class and overriding the methods we will be able to make Checkbox in the following options:
How to use RightCheckBox Class To use RightCheckBox class we need to create a demo java class let's assume RightCheckboxDemo is the name of the demo class which will use the above class to show the desired user interface:


public class RightCheckboxDemo extends BorderPage{
 private Form form = new Form("form");
 private RightCheckBox checkbox1=new RightCheckBox("checkbox1");
 private RightCheckBox checkbox2=new RightCheckBox("checkbox2");
 private RightCheckBox checkbox3=new RightCheckBox("checkbox3");
 private RightCheckBox checkbox4=new RightCheckBox("checkbox4");
 private RightCheckBox checkbox5=new RightCheckBox("checkbox5");
 public RightCheckboxDemo(){
   //Using setLabel we can set the default label and lable will be on left side
  checkbox1.setLabel("Item 1");

 //Using setRightLabel we can set the custom label and lable will be on right side
  checkbox2.setRightLabel("Item 2");
  checkbox3.setRightLabel("Item 3");
  checkbox4.setRightLabel("Item 4");
  checkbox5.setRightLabel("Item 5");

By using the addControl(form) we have provided the form to the htm page. Now we have to right one htm page to render the user interface: RightCheckboxDemo.htm
<head><title>Demo Page</title>
Finally the user interface is ready to render and we can see how is this looks like:

Please share your comments or suggestion. You can visit our website and leave you comment over there. To visit our website visit on:
VaaCom : A Blog on Technology

Microsoft Dynamics CRM - Java Web Client Implementation

CRM - Microsoft Dynamics A Cloud based Customer Relationship Management(CRM) software package developed by Microsoft. Microsoft Dynamics ...