GWT is there a <label> widget?

Viewed 16979

I have to add a form to a small piece of AJAX functionality implemented in GWT. In HTML terms, I'd like

<label for="personName">Name:</label><input type="text" size="50" id="personName"/>

It appears the Label widget in GWT simply renders as a DIV.

Ideally I would like clicking on the label text to focus the associated input. This is built-in browser functionality I don't want to have to mess around with ClickHandlers on label divs!

Has anyone faced this issue? Does exist as a built-in widget but is called something else?

EDIT: Have come up with the following. Maybe there is a better way?

HTML label = new HTML();
label.setHTML("<label for='"+input.getElement().getId()+"'>"+labelText+"</label>");
5 Answers

I had the same need and ended up creating my own Widget

public class Label extends Widget implements HasText {

public Label() {
  setElement(DOM.createLabel());
}

@Override
public void add(Widget w) {
  super.add(w, getElement());
}

@Override
public String getText() {
  return getElement().getInnerText();
}

@Override
public void setText(String text) {
  getElement().setInnerText((text == null) ? "" : text);
}

public void setFor(String forWho) {
  getElement().setAttribute("for", forWho);
}

}

Related