2017-10-09 2 views
0

JxBrowser를 사용하여 Java Javascript 브리지를 설정하려고합니다. onScriptContextCreated 메서드가 호출되지 않아 그 이유가 확실하지 않습니다.JxBrowser onScriptContextCreated가 호출되지 않습니다.

Java 응용 프로그램에 브라우저가 내장되어 있으며 버튼을 클릭하면 "howdy"가 표시되지만 "hello"는 Java 콘솔에 인쇄되지 않습니다. 나는 onScriptContextCreated 방법으로 디버그 라인을 던져 버렸고 절대로 안타.

다음은 자바 코드 여기

webBrowser.addScriptContextListener(new ScriptContextAdapter() {  
      @Override 
      public void onScriptContextCreated(ScriptContextEvent event) { 
      Browser browser = event.getBrowser(); 

      JSValue window = browser.executeJavaScriptAndReturnValue("window"); 
      System.out.println("Setting property"); 
      window.asObject().setProperty("sendNSCommand", new JSFunctionCallback() { 

       @Override 
       public Object invoke(Object... os) { 
        String command = (String) os[0]; 
        System.out.println("Command from JavaScript: " + command); 
        return null; 
       } 
      }); 
     } 
    }); 

그리고이 코드 예제가 제대로 작동처럼

<html> 
<head> 
    <script> 
     function callMe() { 
      sendNSCommand2("howdy"); 
      sendNSCommand("hello"); 

     } 

     function sendNSCommand2(x) { 
      window.alert(x); 
     } 
    </script> 
</head> 
<body> 
<input type="button" value="test" onClick="callMe()" /> 
</body> 
</html> 

답변

0

가 보이는 내 샘플 웹 페이지입니다에게 있습니다. 로컬 환경에서 실행 한 후 "Setting property"행을 Java 콘솔에 인쇄합니다. "test"버튼을 클릭하면 "howdy"대화 상자가 나타납니다. 이 대화 상자에서 "확인"버튼을 클릭하면 "Java에서 명령 : hello"행이 Java 콘솔에 인쇄됩니다.

관련 문제