Java 如何等待SWT浏览器完成加载?
我在应用程序中使用SWT浏览器。我需要在浏览器的HTML页面上运行脚本。但是脚本是在页面完全加载之前运行的。那么,如何让应用程序等待浏览器加载完成呢。我试过这样的东西Java 如何等待SWT浏览器完成加载?,java,browser,swt,Java,Browser,Swt,我在应用程序中使用SWT浏览器。我需要在浏览器的HTML页面上运行脚本。但是脚本是在页面完全加载之前运行的。那么,如何让应用程序等待浏览器加载完成呢。我试过这样的东西 completed = true; browser.addProgressListener(new ProgressListener() { @Override public void completed(ProgressEvent event) { completed = true; //say
completed = true;
browser.addProgressListener(new ProgressListener() {
@Override
public void completed(ProgressEvent event) {
completed = true; //say this is a global variable
}
@Override
public void changed(ProgressEvent event) {
completed = false;
System.out.println("Page changing");
}
});
//some other method
void m1()
{
browser.setText("blah blah blah");
while (completed == false)
{}
// EXECUTE THE SCRIPT NOW
}
但这是行不通的
这与类似,但没有解决方案。您可以定义一个
浏览器函数
,并从JavaScript代码调用它:
public static void main(String[] args)
{
Display display = new Display();
Shell shell = new Shell(display);
shell.setLayout(new FillLayout());
Browser browser = new Browser(shell, SWT.NONE);
new CustomFunction(browser, "theJavaFunction");
browser.setText("<style>#map { width: 100%; height: 300px; }</style><script src='http://maps.google.com/maps/api/js?sensor=false'></script><div id='map'></div><script>var map;function initialize() { var mapOptions = { zoom: 8, center: new google.maps.LatLng(-34.397, 150.644) }; map = new google.maps.Map(document.getElementById('map'), mapOptions);} google.maps.event.addDomListener(window, 'load', initialize);window.onload = function () { theJavaFunction(); };</script>");
shell.pack();
shell.setSize(600, 400);
shell.open();
while (!shell.isDisposed())
{
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
private static class CustomFunction extends BrowserFunction
{
CustomFunction(Browser browser, String name)
{
super(browser, name);
}
@Override
public Object function(Object[] arguments)
{
System.out.println("theJavaFunction() called from javascript");
return null;
}
}
publicstaticvoidmain(字符串[]args)
{
显示=新显示();
外壳=新外壳(显示);
setLayout(新的FillLayout());
浏览器=新浏览器(shell,SWT.NONE);
新的自定义功能(浏览器,“theJavaFunction”);
browser.setText(“#map{width:100%;height:300px;}var-map;function initialize(){var-mappoptions={zoom:8,center:new google.maps.LatLng(-34.397150.644)};map=new google.maps.map(document.getElementById('map'),mappoptions);}google.maps.event.adddomstener(窗口,'load',initialize);window.onload=function(){javafunction()};");
shell.pack();
外壳尺寸(600400);
shell.open();
而(!shell.isDisposed())
{
如果(!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
私有静态类CustomFunction扩展了BrowserFunction
{
自定义函数(浏览器、字符串名称)
{
超级(浏览器、名称);
}
@凌驾
公共对象函数(对象[]参数)
{
println(“从javascript调用的JavaFunction());
返回null;
}
}
Baz给出了正确答案的方向。我将试着把他的答案放在你的上下文中:
private boolean loading; // Instance variable
createBrowserControl() { // Browser widget definition method
Browser b = ...; // Create the browser widget
browser.addProgressListener(new ProgressListener() {
@Override
public void completed(ProgressEvent event) {
loading = false;
}
@Override
public void changed(ProgressEvent event) {
loading = true;
}
});
}
public boolean loadPage(String url) {
Display display = ... // Get the display from the UI or the widget
boolean set = browser.setUrl(url); // URL content loading is asynchronous
loading = true;
while(loading) { // Add synchronous behavior: wait till it finishes loading
if(!display.readAndDispatch()) {
display.sleep();
}
}
return set;
}
建议的解决方案的哪一部分不起作用?在执行browser.setText()之后,while循环立即被执行,然后completed=true,它甚至不会进入循环。例如,如果我在while循环中放置print语句,它甚至不会被打印一次。请查看编辑过的代码。嗯,设置这样的短文本是在瞬间完成的。你为什么需要循环呢?举个例子,我给出了一个短文本,但我在我的应用程序中设置了一个相当长的文本。因此加载确实需要几秒钟。对我的答案有任何反馈吗?