2011-10-26 2 views
4

GWT 2.4를 사용하고 있습니다. 내 onModuleLoad 메서드에서 문자열 id가 주어지면 RootPanel 객체에서 페이지의 기존 버튼에 대한 참조를 얻는 방법은 무엇입니까? 나는 "버튼에 인 RootPanel에서 캐스팅 할 수 없습니다"이GWT : RootPanel에서 버튼에 대한 참조를 얻으려면 어떻게해야합니까?

public void onModuleLoad() { 
    ... 
    final Button submitButton = (Button) RootPanel.get("submit"); 

을 시도하지만 컴파일 오류가납니다.

편집 :

내가 고통을 치유 할 반복자를 사용하여 생각하지,하지만 주사위. 다음로드 기본 HTML (ID = "제출"을 버튼을 통지) ...

<div> 

    <form name="f"> 

     File name: <input type="text" size="25" id="filename" name="filename" 

      value="" /> <input type="button" id="submit" name="submit" 

      value="Submit" /> <input type="hidden" name="curId" id="curId" 

      value="" /> 

    </form> 

</div> 

<div id="content"></div> 

지만는 onModuleLoad에서이 코드는 NullPointerException이 발생합니다 (submitButton ID가 찾을 수 없기 때문에) ...

public void onModuleLoad() { 

    final Button submitButton = (Button) getWidgetById("submit"); 
    submitButton.addStyleName("submitButton"); 
    ... 

private Widget getWidgetById(final String id) { 
    Widget eltToFind = null; 
    final Iterator<Widget> iter = RootPanel.get().iterator(); 
    while (iter.hasNext()) { 
     final Widget widget = iter.next(); 
     final Element elt = widget.getElement(); 
     if (elt.getId() != null && elt.getId().equals(id)) { 
      eltToFind = widget; 
      break; 
     } // if 
    } // while 
    return eltToFind; 
} 

감사합니다, - 데이브

답변

13

Document.get().getElementById("submit").<InputElement>cast()을 사용하여 입력 요소를 가져올 수 있지만 Button 위젯을 가져올 수 없습니다.

, 당신은 Button.wrap()을 사용할 수 있습니다 (그렇게하지 않으면 제출 = 유형 = 버튼 부분은 기술적으로 필요하지 않지만 일부 브라우저는 형처럼 취급합니다) 당신은 <button type="button" id="submit" name="submit" value="Submit"> 대신 <input>을 읽는 코드를 변경하는 경우 :

Button button = Button.wrap(Document.get().getElementById("submit")); 
+0

순금. - Dave – Dave

1

는 GET() 메소드는 브라우저 요소가 아니라 그 이름을 가진 위젯과 관련된 인 RootPanel를 반환합니다. RootPanel은 ComplexPanel의 하위 클래스이므로 ComplexPanel의 메서드를 사용하여 위젯을 반복하고 원하는 방식으로 찾을 수 있다고 생각합니다.

+0

어떻게 수행 할 수 있습니까? 나는 반복자 접근법을 시도했다. 그러나 편집자는 id가 페이지에 확실히있는 요소를 찾지 못했다. – Dave

2

일부 GWT 위젯에는 DOM 요소를 위젯 인스턴스로 변환 할 수있는 정적 메서드 wrap()이 있습니다.

Button submit = Button.wrap (DOM.getElementById ("submit")));

관련 문제