4

may i know how to cast JavascriptObject get from JSNI into gwt as gwt CUstomWidget

CustomWiget widget = (CustomWidget) javascriptObjectFromJSNI; //doesnt work

cometta
  • 35,071
  • 77
  • 215
  • 324

2 Answers2

4

You cannot cast an ELement to a Widget. You can use GQuery to help you out in this case.

GQuery.$(element).widget() will give you the GWT widget you are looking for on your DOM.

Karthik
  • 136
  • 2
  • 11
  • Hi Karthik, while using GQuery error is coming like No source code is available for type com.google.gwt.query.client.GQuery; did you forget to inherit a required module?...What can I do now... – nmkyuppie Mar 12 '14 at 09:26
2

You can use CustomWidget as the return type of your JSNI method. The example at http://code.google.com/webtoolkit/doc/latest/DevGuideCodingBasicsOverlay.html#example-json does this with Customer (which extends JavaScriptObject):

private native Customer getFirstCustomer() /*-{
  return $wnd.jsonData[0];
}-*/;

If your CustomWidget is really a Widget (not a JavaScriptObject), then you're probably looking for somthing quite different: In that case you'd have to write a wrap() method like com.google.gwt.user.client.ui.Button.wrap().

Chris Lercher
  • 37,264
  • 20
  • 99
  • 131
  • CustomWidget is gwt custom widget, not json. can you elaborate more on wrap() ? – cometta Jan 16 '11 at 14:15
  • @cometta: Well, you'd use something like `com.google.gwt.user.client.DOM.getElementById(String)` or some other method to retrieve a com.google.gwt.user.client.Element, and then feed it into your own wrap() method, which you'd have to write in the style of Button.wrap(), Anchor.wrap(), Label.wrap(), ... – Chris Lercher Jan 16 '11 at 14:22
  • I looked through Label source code. and I do similar and able to get the element. but after i done new CustomWidget(Element), and trying to get the value user entered, it's null. is it possible to get user entered value through wrap() method to return customwidget? – cometta Jan 16 '11 at 15:19
  • please see http://stackoverflow.com/questions/4706271/gwt-custom-widget-wrap-method – cometta Jan 16 '11 at 15:44