Showing posts with label gwt button click problem. Show all posts
Showing posts with label gwt button click problem. Show all posts

Monday, June 1, 2009

Button.click() não funciona


Problema

Ao chamar o método Button.click() (classe do GWT) nada acontece. Ex:

...
Button button = new Button("Button");

button.addClickHandler(new ClickHandler(){
@Override
public void onClick(ClickEvent event) {
throw new RuntimeException("RuntimeException");
}});

button.click();
...

Este código, a princípio, deveria lançar uma exceção.

Motivo

O botão, ou um objeto que o contém, não foi adicionado ao RootPanel.

Solução

Adicionar o widget ao RootPanel. Ex:

...
Button button = new Button("Button");

RootPanel.get().add(button);

button.addClickHandler(new ClickHandler(){
@Override
public void onClick(ClickEvent event) {
throw new RuntimeException("RuntimeException");
}});

button.click();
...

Mais: