Default value in empty TextBox

1,941 views
Skip to first unread message

Brock Parker

unread,
Feb 7, 2008, 9:10:17 AM2/7/08
to Google Web Toolkit
Hello,

Is there anything built into GWT to display a value in a TextBox if
the field is emtpy? This value would disappear once the user clicked
in the field and started typing. I have seen this functionality on
several Google web pages (i.e. the Contacts page in GMail) but I can't
find anything in the documentation or samples about it.

Thanks,

Brock

Sean

unread,
Feb 8, 2008, 7:17:53 AM2/8/08
to Google Web Toolkit
You could probably use a focus listener and onFocus clear the text,
onLostFocus put it back to it's default.

Sean

unread,
Feb 8, 2008, 7:49:39 AM2/8/08
to Google Web Toolkit
I thought this would be a nice thing to have in my toolbox so I made
one. This works just like the Gmail Contacts text box. You can edit it
and do what you see fit to get it to work how you like:

public class DefaultTextBox extends TextBox implements FocusListener {
private String defaultText;

public DefaultTextBox(String defText) {
defaultText = defText;
setText(defaultText);
addFocusListener(this);
}

public void setDefaultText(String defText) {
defaultText = defText;
}

public String getDefaultText() {
return defaultText;
}

public void onFocus(Widget sender) {
this.setText("");

}

public void onLostFocus(Widget sender) {
this.setText(defaultText);
}

}

I instantiated it like:
DefaultTextBox tb = new DefaultTextBox("Search, add, or invite.");

Brock Parker

unread,
Feb 8, 2008, 9:25:11 AM2/8/08
to Google Web Toolkit
Thanks Sean, that's what I was planning to do myself if nobody else
came up with anything.

Jason Essington

unread,
Feb 8, 2008, 10:44:09 AM2/8/08
to Google-We...@googlegroups.com
I usually do something similar, but additionally add a styleName to
the box that allows the default text to be styled in a way that is
visually different from user entered text (gray vs black for instance)
and clear that style when the user begins typing in the field.

-jason

Peter Blazejewicz

unread,
Feb 9, 2008, 2:43:56 PM2/9/08
to Google Web Toolkit
hi Brock,
that could be done only with JavaScript (as in exampel), there is no
default value attr or property for text inputs:
http://www.w3schools.com/tags/tag_input.asp

regards,
Peter

Christopher

unread,
Mar 19, 2008, 2:25:53 AM3/19/08
to Google Web Toolkit
Thanks for the code. Here's my extension of it:

public class DefaultTextBox extends TextBox implements FocusListener {
String defaultText;
boolean defaultTextMode = false;

public DefaultTextBox(String defaultText) {
setDefaultText(defaultText);
setDefaultTextMode();
addFocusListener(this);
}

public String getDefaultText() {
return defaultText;
}

public String getText() {
if (!defaultTextMode) {
return super.getText();
} else {
return "";
}
}

public void onFocus(Widget sender) {
if (defaultTextMode) {
setNormalTextMode();
}
}

public void onLostFocus(Widget sender) {
if (getText().length() == 0) {
setDefaultTextMode();
}
}

public void setDefaultText(String defaultText) {
this.defaultText = defaultText;
if (defaultTextMode) {
setDefaultTextMode(); // Refresh
}
}

void setDefaultTextMode() {
assert super.getText().length() == 0;
super.setText(defaultText);
addStyleDependentName("default");
defaultTextMode = true;
}

void setNormalTextMode() {
assert super.getText().length() != 0;
super.setText("");
removeStyleDependentName("default");
defaultTextMode = false;
}

public void setText(String text) {
super.setText(text);
if (text.length() == 0) {
setDefaultTextMode();
} else {
setNormalTextMode();
}
}
}

Cheers,
-C

Zied Hamdi

unread,
Nov 27, 2013, 3:39:21 AM11/27/13
to google-we...@googlegroups.com, Google Web Toolkit
Hi all,

There is ready version (among other widgets) in the buttom of this page 


Best regards

Moutellou

unread,
Nov 27, 2013, 8:24:13 AM11/27/13
to google-we...@googlegroups.com, Google Web Toolkit, bpar...@gmail.com
You can try the attribute placeholder.
getElement().setPropertyString("placeholder", "default value")

Ismael Hasan

unread,
Aug 7, 2014, 8:49:26 AM8/7/14
to google-we...@googlegroups.com, Google-We...@googlegroups.com, bpar...@gmail.com
Kudos for Moutellou, I find his solution very clean.

HTML already provides the capability to fulfil the initial requirement, so I think it is best taking advantage of that instead of dealing with more GWT artifacts and re-building a functionality that already exists.
Reply all
Reply to author
Forward
0 new messages