1

I want to set a watermark/placeholder inside of a GWT DateBox. I know how to use onFocus and onBlur to set up a watermark/placeholder in a normal TextBox. I assumed that doing so in a DateBox would be relatively similar. Setting up the text currently looks like this, but does nothing at all.

    Datebox box = new DateBox();
    box.getTextBox().setText("mm/dd/yyyy");

Is there a reason that this would not be working?

Chris Cashwell
  • 22,308
  • 13
  • 63
  • 94
Duck
  • 35
  • 4

2 Answers2

1
box.getTextBox().setValue("mm/dd/yyyy");
Chris Boesing
  • 5,239
  • 3
  • 26
  • 30
  • Even after trying this, the DateBox shows no text. As a matter of curiosity, what is the difference between setText and setValue in a TextBox (sorry for the noob question)? – Duck Jun 22 '11 at 14:41
  • I have just tried it and it works, are you over writing it at some point? – Chris Boesing Jun 22 '11 at 23:24
  • @chris `setText` is inherited from `HasText`, and really just passes the value through to `setValue`. – Chris Cashwell Dec 14 '11 at 22:28
0

I imagine what you were actually talking about here was being able to set placeholder text. I posted a solution for TextBox elements here before. The process would be quite similar:

public class DateField extends DateBox {

  String placeholder = "";

  /**
   * Creates an empty DateField.
   */
  public DateField() {}

  /**
   * Gets the current placeholder text for the date box.
   * 
   * @return the current placeholder text
   */
  public String getPlaceholder() {
      return placeholder;
  }

  /**
   * Sets the placeholder text displayed in the date box.
   * 
   * @param placeholder the placeholder text
   */
  public void setPlaceholder(String text) {
      placeholder = (text != null ? text : "");
      getElement().setPropertyString("placeholder", placeholder);
  }
}

Then replace your DateBox objects with DateField objects, and you'd just call someDateField.setPlaceholder("mm/dd/yyyy");.

Community
  • 1
  • 1
Chris Cashwell
  • 22,308
  • 13
  • 63
  • 94