Note that there are some explanatory texts on larger screens.

plurals
  1. POGWT - setText and setValue not working for TextBox on initial load
    primarykey
    data
    text
    <p>I have a couple of text-boxes on a page where the user can enter some numeric values; however, try as I might, I can't fill those text-boxes with default values - specifically I would like <code>0.0</code> displayed in both upon page load.</p> <p>Here is how I create them and what I have tried -</p> <pre class="lang-java prettyprint-override"><code>GroupSection engineering_group = new GroupSection(); KSTextBox engrDesignTextBox = new KSTextBox(); engrDesignTextBox.setWidth("2.875em"); //engrDesignTextBox.setWatermarkText("0.0"); ==&gt; this works, but not what I need //engrDesignTextBox.setText("0.0"); ==&gt; this doesn't work engrDesignTextBox.setValue("0.0"); // doesn't work either KSTextBox engrScienceTextBox = new KSTextBox(); engrScienceTextBox.setWidth("2.875em"); //engrScienceTextBox.setWatermarkText("0.0"); ==&gt; this works, but not what I need //engrScienceTextBox.setText("0.0"); ==&gt; this doesn't work engrScienceTextBox.setValue("0.0"); // doesn't work either </code></pre> <p>I'm thinking that I need to attach an "onload" event listener and then try the setText in there? That seems overkill for something that should be rather simple.</p> <p>Incidentally, I have attached onBlurHandlers for both these text boxes and they work as expected (see code below)</p> <p>The following code will simply insert<code>0.0</code> if the user clicks or tabs out of the text-box while it is EMPTY. </p> <pre class="lang-java prettyprint-override"><code>engrDesignTextBox.addBlurHandler(new BlurHandler() { @Override public void onBlur(BlurEvent blurEvent) { if(((KSTextBox)blurEvent.getSource()).getText().length() &lt; 1) { ((KSTextBox)blurEvent.getSource()).setText("0.0"); } } }); engrScienceTextBox.addBlurHandler(new BlurHandler() { @Override public void onBlur(BlurEvent blurEvent) { if(((KSTextBox)blurEvent.getSource()).getText().length() &lt; 1) { ((KSTextBox)blurEvent.getSource()).setText("0.0"); } } }); </code></pre> <p><strong>EDIT :</strong> As requested here is how I have defined the setText and setValue methods in KSTextBox</p> <pre class="lang-java prettyprint-override"><code>public class KSTextBox extends TextBox implements HasWatermark { . . . @Override public void setText(String text) { String oldValue = super.getText(); if(hasWatermark) { if(text == null || (text != null &amp;&amp; text.isEmpty())){ super.setText(watermarkText); addStyleName("watermark-text"); watermarkShowing = true; } else{ super.setText(text); removeStyleName("watermark-text"); watermarkShowing = false; } } else{ super.setText(text); } ValueChangeEvent.fireIfNotEqual(this, oldValue, text); } @Override public void setValue(String value) { if(hasWatermark) { if(value == null || (value != null &amp;&amp; value.isEmpty())){ super.setValue(watermarkText); addStyleName("watermark-text"); watermarkShowing = true; } else{ super.setValue(value); removeStyleName("watermark-text"); watermarkShowing = false; } } else{ super.setValue(value); } } </code></pre> <p>So, getting back to the original question, how I do I initially set the values for these textboxes to <code>0.0</code>?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload