user3227175
user3227175

Reputation: 29

Enable/Disable form elements and changing the values of button at a time?

Requirement: there is a button with text "disable". I want it to be disabled when page opens. When the user clicks on the button, I want my form fields to get enabled and at the same time I want my button value to be change to "enabled".

I have an example of how to change the value of the button, but how do I enable/disable it at the same time?

<h:form>
  Please press the Button it.
  <h:commandButton id="cb1" value="#{ActionBean.buttonText}"
    immediate="true" actionListener="#{ActionBean.processMyAction}" />
</h:form>

My bean class looks like this:

/* Action listener method */
public void processMyAction(ActionEvent event) {
    disable = !disable;
    System.out.println("disable: " + disable);
    if (buttonText.equals("Disable")) {
        buttonText = "Enable";
        System.out.println("buttonText: " + buttonText);
    } else {
        buttonText = "Disable";
        System.out.println("buttonText: " + buttonText);
    }

Upvotes: 1

Views: 1298

Answers (1)

Adarsh
Adarsh

Reputation: 3641

Here is an example for you to try.

    <h:body>
        <h:form>
           <h:inputText disabled="#{myBean.disabled}"></h:inputText>
           <h:commandButton value="#{myBean.buttonName}" actionListener="#{myBean.changeButton}"></h:commandButton>
        </h:form>
    </h:body>

And the bean,

public class MyBean {    
    public MyBean(){
       this.disabled = true;
       this.buttonName = "Edit";
    }

    public String getButtonName() {
       return buttonName;
    }

    public void setButtonName(String buttonName) {
          this.buttonName = buttonName;
    }
    private String buttonName;

    public void changeButton(ActionEvent event){
        if(this.buttonName.equals("Edit")){
            this.disabled = false;
            this.buttonName = "Save";
        }else{
            this.disabled = true;
            this.buttonName = "Edit";
        }       
    } 
     public boolean isDisabled() {
         return disabled;
     }

     public void setDisabled(boolean disabled) {
          this.disabled = disabled;
     }

     private boolean disabled;   
}

Upvotes: 1

Related Questions