Samade
Samade

Reputation: 31

How to change text label in wicket

I'm trying to change the text on a button in wicket using AjaxLink but it is not working, any help please? here is my code;

<form name="depForm" wicket:id="depSummaryForm" action=""> 
<div class="uw_summary"> <div class="buttonContainer"> 
<span class="button_v2" wicket:id="newRecruitContainer"> 
<button class="internal_button" wicket:id="newRecruitEWLButton" 
type="button"> Create New Recruit EWL </button> </span> 

final WebMarkupContainer newRecruitContainer = new 
WebMarkupContainer("newRecruitContainer")
{
Label buttonlabel = new Label("newRecruitEWLButton", Model.of(" "));
buttonlabel.setOutputMarkupPlaceholderTag(true);

AjaxLink newRecruitEWLButton = new AjaxLink("newRecruitEWLButton") { 
@Override public void onClick(AjaxRequestTarget target) { 
buttonlabel.setDefaultModelObject(Model.of("Creating EWL")); 
target.add(buttonlabel); boolean isWorkItemCreated = 
NewRecruitEWLUtil.createNewRecruitWorkItem(appInfo); } };  

Upvotes: 0

Views: 1691

Answers (2)

Samade
Samade

Reputation: 31

using a div for the button label in the html file instead of a span works, and calling this method from the onclick setDefaultModel(); this method does not work in this sceanario - setDefaultModelModelObject()

Upvotes: 0

martin-g
martin-g

Reputation: 17533

You need to update the Label's model, not to add a new Label.

WebMarkupContainer xyzContainer = new WebMarkupContainer("xyzContainer") {

   @Override
   public void onConfigure() {  // [1]
      super.onConfigure();
      boolean isVisible = some code goes here
      setVisible(isVisible);
   }
};

final Label buttonlabel = new Label("clickMe", ""); // [2]
buttonlabel.setOutputMarkupId(true); // [3]

AjaxLink xyzButton = new AjaxLink("xyzButton") {
   @Override
   public void onClick(AjaxRequestTarget target) {         
       buttonlabel.setModelObject("Clicked")); // [4]
       target.add(buttonlabel);
   }
};


xyzContainer.add(xyzButton);
Form.add(xyzContainer);

Here are my suggested changes:

  1. Use onConfigure() + setVisible() because isVisible() is called many times and depending on how expensive is "some code goes here" it may affect the overall performance
  2. Sets an initial label - empty string
  3. setOutputMarkupPlaceholderTag() should be used when the Component could be set to invisible. If it is always visible then setOutputMarkupId(true) is enough
  4. Use Component.setModelObject(...) to set a new value of the same Label instance

Update: Here is how your code should look like:

<form name="depForm" wicket:id="depSummaryForm"> 
   <div class="uw_summary"> 
     <div class="buttonContainer"> 
       <span class="button_v2" wicket:id="newRecruitContainer"> 
         <button class="internal_button" wicket:id="newRecruitEWLButton" 
type="button">
 <span wicket:id="label">Create New Recruit EWL</span></button> 
       </span>
     </div>
   </div>
</form>


final WebMarkupContainer newRecruitContainer = new 
WebMarkupContainer("newRecruitContainer");

Label buttonlabel = new Label("label", Model.of(" "));
   buttonlabel.setOutputMarkupPlaceholderTag(true);

   AjaxLink newRecruitEWLButton = new AjaxLink("newRecruitEWLButton") 
   { 
      @Override public void onClick(AjaxRequestTarget target) { 
          buttonlabel.setDefaultModelObject(Model.of("Creating EWL")); 
          target.add(buttonlabel); 
          boolean isWorkItemCreated = 
             NewRecruitEWLUtil.createNewRecruitWorkItem(appInfo); } 
   };
   newRecruitContainer.add(newRecruitEWLButton);
   newRecruitEWLButton.add(buttonlabel);

Upvotes: 1

Related Questions