Wicket: How to change the label text to textarea onkeyup?

How to change label text to textarea onkeyup? I tried this but it 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") ); } } 

Decision

leonidv was almost there. Received code:

 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() , which for some reason kept the label intact.

By the way, the result will soon look like an isolated JTexy sandbox .

Thanks for the help!

+4
source share
2 answers

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

  • Updated components must have the setOutputMarkupId == true; flag setOutputMarkupId == true;
  • You must add these components to the onEvent target 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 do not understand many parts of your code.

+4
source

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

0
source

Source: https://habr.com/ru/post/1299101/


All Articles