views:

747

answers:

2

Hi,

How do I change label's text on textarea's onkeyup? I've tried this but does not work:

Form form;
TextArea ta;
MyLabel resultDiv;


  /**
   * Constructor that is invoked when page is invoked without a session.
   */
  public HomePage(final PageParameters parameters) {

      this.form = new Form("form");
      this.ta = new TextArea("text");
      this.resultDiv = new MyLabel("result");

      this.ta.add( new AjaxEventBehavior( "onKeyUp" ) {
        protected void onEvent( AjaxRequestTarget target ) {
          System.out.println( "Ajax!" );
          resultDiv.setText("Foobar");
          resultDiv.renderComponent();
        }
      } );


      form.add( ta );
      form.add( resultDiv );
      add( form );

  }// const

  public class MyLabel extends Label {
    private String text = "original";
    public String getText() {      return text;    }
    public void setText( String text ) {      this.text = text;    }
    public MyLabel( String id ) {
      super( id );
      this.setModel( new PropertyModel(this,"text") );
    }
  }

Solution

leonidv was almost there. The resulting code is:

Form form;
TextArea ta;
Label resultDiv = new Label( "result", new PropertyModel(this,"labelText") ){
  { setOutputMarkupId( true ); }
};

private String labelText = "original";


/**
 * Constructor that is invoked when page is invoked without a session.
 */
public HomePage(final PageParameters parameters) {

    this.form = new Form("form");

    this.ta = new TextArea("text");
    this.ta.add( new AjaxEventBehavior( "onKeyUp" ) {
      protected void onEvent( AjaxRequestTarget target ) {
        System.out.println( "Ajax!" );
        labelText = "Foobar";  // Doesn't even need get/set, which is great.
        target.addComponent( resultDiv );
        //resultDiv.renderComponent(); // WRONG!!
      }
    } );

    form.add( ta );
    form.add( resultDiv );
    add( form );

}// const

The last problem was my bad intuition about adding renderComponent() - that, for some reason, kept the label unchanged.

By the way, the result will serve soon as JTexy lightweight markup language sandbox.

Thanks for help!

A: 

instead of resultDiv.renderComponent(); try resultDiv.modelChanged();

Paul Szulc
Didn't work; IMHO `modelChanged()` is a callback to be overriden (just guessing from the name).
Ondra Žižka
+3  A: 

If you want to update components after AJAX event, you must to do 2 things:

  1. Updatable components must have setted flag setOutputMarkupId == true;
  2. You must add this components to target onEvent method

    this.resultDiv.setMarkupOutputId(true);

    protected void onEvent( AjaxRequestTarget target ) { System.out.println( "Ajax!" ); //resultDiv.setModel( ); resultDiv.setText("Foobar"); resultDiv.renderComponent(); target.add(resultDiv); }

PS I don't understand many parts of your code.

leonidv
> I don't understand many parts of your code.-- I do neither :) I'm new to Wicket and it's not exactly intuitive.
Ondra Žižka
Very great wicket feature is binding fields to components. So, you don't need to "public class MyLabel extends Label". All is requested to create PropertyModel:MyPage .. { String text = "original"; .... in constructor .... new Label("id",new PropertyModel(this,"text");}After this wicket bind label and text field. So, when you process submit or AJAX event you don't need to write smth like a:text = label.getValue()System.out.println(text);wicket to this automatically, just write:sysout(text);
leonidv
You may be interested in this article:http://cwiki.apache.org/WICKET/working-with-wicket-models.html
leonidv
Last question - how can I get the content of the textarea when processing the event? Its model is not updated (still contains what I initialized to).
Ondra Žižka
Try this link: http://wicket.apache.org/exampleguestbook.html I think you can bind content TextArea to other field, and in ajax behavior labelText = contentText. It's more interested try to bind labelText also to TextArea, or even bind Label.getValue to TextArea.
leonidv