我想在DJ native swing jwebbrowser中执行我的javascript代码,但是我应该等待页面加载,我该如何为它设置监听器? 我尝试以下,但加载后它总是运行脚本,它也运行得很晚!
webBrowser.addWebBrowserListener(new WebBrowserListener(){
@Override
public void commandReceived(WebBrowserCommandEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void loadingProgressChanged(WebBrowserEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void locationChangeCanceled(WebBrowserNavigationEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void locationChanged(WebBrowserNavigationEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void locationChanging(WebBrowserNavigationEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void statusChanged(WebBrowserEvent arg0) {
// TODO Auto-generated method stub
if(webBrowser.getLoadingProgress() == 100)
webBrowser.executeJavascript("some js code");");
}
@Override
public void titleChanged(WebBrowserEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void windowClosing(WebBrowserEvent arg0) {
// TODO Auto-generated method stub
frame.dispose();
}
@Override
public void windowOpening(WebBrowserWindowOpeningEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void windowWillOpen(WebBrowserWindowWillOpenEvent arg0) {
// TODO Auto-generated method stub
}
});
我该怎么办?
答案 0 :(得分:2)
我应该覆盖loadProgressChanged(WebBrowserEvent arg0)而不是statusChanged(WebBrowserEvent arg0)
@Override
public void loadingProgressChanged(WebBrowserEvent arg0) {
// TODO Auto-generated method stub
if(webBrowser.getLoadingProgress() == 100)
webBrowser.executeJavascript("some js code");
}
但是加载我的html仍然需要时间!