views:

213

answers:

3
+1  Q: 

text in text box

hi, i am pretty new to the gwt framework and i am using it for building the ui of my web site, i would like to make the text box have a text in it that once the user clicks on it for the first time, the text disappears. and in the rest of the time it behaves like a normal text box

any ideas on how to do it?

+1  A: 

Hi, you can add a clickHandler to the box. Within the handler you do something as easy as:

if(text==DEFAULT_TEXT) { text=="" }

If someone is going to write again the same DEFAULT_TEXT it would get wiped out again. If you want to avoid that add boolean variable in the check expression.

Tarelli
A: 

Can't tell it for GWT, but a general approach could be:

  1. use a variable to flag, whether the text box is 'initialized' or 'in use'
  2. add a listener to the text widget (I'd use a KeyboardListener and make the text disappear when the user starts entering text and not on the first - maybe accidental - mouse click)
  3. When the listener receives the first event for the widget (flag = 'initialized'), clear the flag and replace the text inside the text field with the actual keystroke.

(for a click listener: upon the first click on the widget clear the flag and the text box.)

Andreas_D
+2  A: 

When you create the textbox, set the default text and add a keyboard listener:

TextBox box = new TextBox();
box.setText("Default Text");
box.addKeyboardListener(this);
defaultValue = true; // this is a global boolean value

Then have your class implement KeyboardListener leaving them all blank except:

public void onKeyPress(Widget arg0, char arg1, int arg2) 
{
    if(defaultValue)
    {
        box.setText = "";
        defaultValue = false;
    }
}
Amber Shah
ye i was about doing something similar, but i thought it is such a common use of textbox that for sure there is something out of the box for that ... any way thanks a lot for the code sample
special0ne