Monday, 20 June 2016

How to use AjaxButton component ?


Using AjaxButton component 

We want to disable a text field and dropdown field  after click on submit button without refreshing the page.

We can modify the Button functionality by AjaxButton.

WelcomePage.java
public class WelcomePage extends WebPage {
   private List<String> genderChoice = new ArrayList<String>();

   public WelcomePage() {
      genderChoice.add("male");
      genderChoice.add("female");
             
      final UserModel userModel = new UserModel();
      Form<?> form = new Form<Object>("form");
             
      final TextField<String> textFieldnew TextField<String>
             ("text", new PropertyModel<String>(userModel, "name"));

      final DropDownChoice<String> gender
             new DropDownChoice<String>("gender"
               new PropertyModel<String>(userModel, "gender") , 
               genderChoice);
             
      AjaxButton ajaxButton = new AjaxButton("submit") {
        private static final long serialVersionUID = 1L;

        @Override
        protected void onSubmit(AjaxRequestTarget target, Form<?> form) {
            super.onSubmit();
            textField.setEnabled(false);
            gender.setEnabled(false);
            target.addComponent(textField);
            target.addComponent(gender);
       
                    
       };
             
             
       /*Button button = new Button("submit"){
           public void onSubmit() {
               super.onSubmit();
               System.out.println("Name: "+userModel.getName());
               System.out.println("Gender"+userModel.getGender());
           };
         }; */
             
       add(form);
             
       form.add(textField);
       form.add(gender);
       form.add(ajaxButton);
   }
}

No comments:

Post a Comment

Note: only a member of this blog may post a comment.