You must pass the source object to the expected object. The getSource () method provides you only an object, but you cannot access any information from it (for example, using a button, you first need to click it on the button).
Here is an example:
Button bt = new Button("click"); bt.addClickHandler(new ClickHandler() { @Override public void onClick(ClickEvent event) { Object soruce = event.getSource(); if (soruce instanceof Button) {
This, of course, also works for UiBinder buttons:
@UiHandler("button") void onClick(ClickEvent e) { Object soruce = e.getSource(); if(soruce instanceof Button){ String buttonText = ((Button)soruce).getText(); RootPanel.get().add(new Label(buttonText)); } else { RootPanel.get().add(new Label("The event is not bound to a button")); } }
If you do not know the type of your element or the event is tied to several elements, you must first check all possible types, and then perform the correct action.
source share