1

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);
    }
user3227175
  • 29
  • 1
  • 10
  • Your requirements are not clear. You want a button to have the text disabled and also disable the button ? – Adarsh Feb 18 '14 at 08:37
  • @Adarsh I think I messed this up in my edit. Thinking it through it's meant to say "I want it to say 'disabled' when the page opens.", ie the user clicks the button which then gets disabled and it's value changed to "enabled" (at least that's the only way this can make sense). – mabi Feb 18 '14 at 08:39
  • @mabi In that case there will be no longer any use for the button after it shows enabled as the button itself would be disabled. That doesn't really confirm with the code that he has provided us. – Adarsh Feb 18 '14 at 08:42
  • @adarsh yes for example say there is a edit button first that should be disabled on click of the edit button i want my form elements to be enabled and onclick of button the button value should change to save – user3227175 Feb 18 '14 at 08:52
  • So the button is enabled at all times ? Only the text of the button changes on each click. Is that right ? – Adarsh Feb 18 '14 at 08:55
  • no the button is enabled only and only on click that same button which is edit in my case – user3227175 Feb 18 '14 at 08:58
  • 1
    Ok. So here is what I understand. You have 1 button on your page. When the page loads, you have 1 button with the text enable and the button enabled. When you click the button, the enable button text is changed to disable and the form fields should be editable. When you again click on the button, the button text is changed to disable and the form elements are disabled. – Adarsh Feb 18 '14 at 10:07
  • @adarsh: i want the other way round first the button name should be edit and it should be disabled on click of of that disabled button edit i want form elements to get enabled and the button value should be changed to save/u can replace the buuton with save button – user3227175 Feb 18 '14 at 10:19
  • You cannnot click a disabled button. That is the whole point of making it disabled. – Adarsh Feb 18 '14 at 10:22
  • @adarsh:ok then let the edit button be enable and the jsf form elements disabled on clicking edit button the form should get enabled and the value of button should be changed to save plz help me out by explaining with an example – user3227175 Feb 18 '14 at 10:26

1 Answers1

1

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;   
}
Adarsh
  • 3,613
  • 2
  • 21
  • 37