javazk

How Customize Component Label in ZK


I want to custom Label component in zk and I need to add a property which is mandatory property when I set mandatory="true" the asterix symbol will be appear and if I set mandatory="false" the asterix symbol disappear, and I am trying like this:

private Label label;
    private Label sign;
    private String lblValue;
    private String REQUIRED_SIGN = " *";
    private boolean mandatory;
   

    public SignLabelCustom() 
        {
        label = new Label();
        label.setSclass("form-label");
        appendChild(label);
        sign = new Label();
        if(mandatory=true){
         sign.setValue(REQUIRED_SIGN);   
         sign.setStyle("color: red");
         appendChild(sign); 
        }
        else{
         sign.setValue("");   
         sign.setStyle("color: red");
        removeChild(sign); 
        }
       
    }

    public String getValue() {
        return lblValue;
    }

    public boolean isMandatory() {
        return mandatory;
    }

    public void setMandatory(boolean mandatory) {
        this.mandatory = mandatory;
    }

   
    
    public void setValue(String lblValue) {
        label.setValue(lblValue);
        this.lblValue = lblValue;
    }

but the condition doesn't working, how to solve it?


Solution

  • What you probably want is called an HtmlMacroComponent, which combines a label and a textbox...

    You start with a zul file:

    <zk>
    <label id="mcLabel"/><textbox id="mcTextbox"/>
    </zk>
    

    ...and create a component for it...

    public class MyTextbox extends HtmlMacroComponent {
    
        @Wire("#mcTextbox")
        private Textbox textbox;
    
        @Wire("#mcLabel")
        private Label label;
    
        private String caption;
    
        private boolean mandatory;
    
        public MyTextbox() {
            compose(); // this wires the whole thing
        }
    
        public void setMandatory(final boolean value) {
            mandatory = value;
            updateCaption();
        }
    
        public boolean isMandatory() {
            return mandatory;
        }
    
        public void setCaption(final String value) {
            caption = value;
            updateCaption();
        }
    
        public String getCaption() {
            return caption;
        }
    
        protected void updateCaption() {
            label.setValue(mandatory ? caption + "*" : caption);
        }
    
        public String getValue() {
            return textbox.getValue();
        }
    
        public void setValue(final String value) {
            textbox.setValue(value);
        }
    
    }
    

    ...and now you can use it, for example by defining it on the top of your zul file... (adjust package and .zul name as required):

    <?component name="mytextbox" macroURI="/zk/textbox.zul" class="com.example.MyTextbox"?>
    

    ...so you can simply use it...

    <mytextbox id="name" value="Frank N. Furter" caption="Your name" mandatory="true"/>
    

    Later you can define a language addon for it...

    my-language-addon xul/html mytextbox com.example.MyTextbox /zk/textbox.zul

    ...so that you don't need to put the definition on top of every .zul file where you use it anymore. See the documentation for more on this.

    Of course, you also could only create a new label, etc. but I found it's a good think to create MacroComponents for those jobs that combine various components, since this way, for example, you could also automatically add validation, etc.