Sunday, May 12, 2024
 Popular · Latest · Hot · Upcoming
174
rated 0 times [  180] [ 6]  / answers: 1 / hits: 16582  / 15 Years ago, thu, july 16, 2009, 12:00:00

I am writing a GWT app that involves interacting with an external document in an iframe. As a proof of concept, I am trying to attach a click handler to a button.



The following works in javascript



var iframe = document.getElementById(rawJSIFrame);
var doc = iframe.contentDocument;
var body = doc.body;
var button = doc.getElementsByTagName(input).namedItem(submit);
button.onclick = function() {
alert(Clicked!);
};


Trying to do the equivalent in GWT, I did the following:



public void addClickHandlerToSubmitButton(String buttonElementName, ClickHandler clickHandler) {
IFrameElement iframe = IFrameElement.as(frame.getElement());
Document frameDocument = getIFrameDocument(iframe);
if (frameDocument != null) {
Element buttonElement = finder(frameDocument).tag(input).name(buttonElementName).findOne();
ElementWrapper wrapper = new ElementWrapper(buttonElement);
HandlerRegistration handlerRegistration = wrapper.addClickHandler(clickHandler);
}
}

private native Document getIFrameDocument(IFrameElement iframe)/*-{
return iframe.contentDocument;
}-*/;


The following is the ElementWrapper class:



public class ElementWrapper extends Widget implements HasClickHandlers {

public ElementWrapper(Element theElement) {
setElement(theElement);
}

public HandlerRegistration addClickHandler(ClickHandler handler) {
return addDomHandler(handler, ClickEvent.getType());
}


}


The code to find the button works fine but the actual click event handler is not getting invoked. Has anybody had a similar issue before, and how did you resolve it?



Thanks in advance,



Tin


More From » java

 Answers
40

Hilbrand is right about the problem being that the GWT method onAttach() was not called.



I implemented your original solution, adding the following method to ElementWrapper:



  public void onAttach() {
super.onAttach();
}


And called added wrapper.onAttach() after the ElementWrapper is created. Works like a charm!


[#99113] Sunday, July 12, 2009, 15 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
sophiak

Total Points: 242
Total Questions: 90
Total Answers: 103

Location: Liechtenstein
Member since Wed, Dec 8, 2021
2 Years ago
sophiak questions
;