在 JavaFX WebView 中检测 URL 变化
Detect URL change in JavaFX WebView
在 JavaFX 的 WebView
中,我正在努力检测 URL 中的变化。
我在 class:
中有这个方法
public Object urlchange() {
engine.getLoadWorker().stateProperty().addListener(new ChangeListener<State>() {
@Override
public void changed(ObservableValue ov, State oldState, State newState) {
if (newState == Worker.State.SUCCEEDED) {
return engine.getLocation());
}
}
});
}
我正在尝试将它用于一个名为 loginbrowser 的对象,例如:
System.out.print(loginbrowser.urlchange());
你能看出我做错了什么吗?
(部分)你做错了什么
您在问题中提供的代码甚至无法编译。 ChangeListener 的 changed 方法是一个空函数,它不能 return 任何值。
无论如何,在 Web 视图中加载内容是一个异步过程。如果您想要在加载 Web 视图后获取 Web 视图位置的值,则需要等待加载完成(在 JavaFX 应用程序线程上不可取,因为这会挂起您的应用程序直到加载完成),或者在回调中收到加载完成的通知(这是您的侦听器正在做的事情)。
(大概)你想做什么
将一些 属性 绑定到网络引擎的位置 属性。例如:
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.VBox;
import javafx.scene.web.*;
import javafx.stage.Stage;
public class LocationViewer extends Application {
@Override
public void start(Stage stage) throws Exception {
Label location = new Label();
WebView webView = new WebView();
WebEngine engine = webView.getEngine();
engine.load("http://www.fxexperience.com");
location.textProperty().bind(engine.locationProperty());
Scene scene = new Scene(new VBox(10, location, webView));
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
launch(args);
}
}
以上代码将在 web 视图的位置发生变化时更新位置标签(通过 运行 代码然后单击一些链接来尝试)。如果您希望仅在页面成功加载后更新标签,那么您需要一个基于 WebView 状态的侦听器,例如:
import javafx.application.Application;
import javafx.concurrent.Worker;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.VBox;
import javafx.scene.web.*;
import javafx.stage.Stage;
public class LocationAfterLoadViewer extends Application {
@Override
public void start(Stage stage) throws Exception {
Label location = new Label();
WebView webView = new WebView();
WebEngine engine = webView.getEngine();
engine.load("http://www.fxexperience.com");
engine.getLoadWorker().stateProperty().addListener((observable, oldValue, newValue) -> {
if (Worker.State.SUCCEEDED.equals(newValue)) {
location.setText(engine.getLocation());
}
});
Scene scene = new Scene(new VBox(10, location, webView));
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
launch(args);
}
}
如果您 运行 最后一个程序并单击某些链接,您会注意到它会延迟位置标签的更新,直到您单击的页面完全加载完毕,这与第一个程序相反一旦位置发生变化,就会更新标签,无论加载是否需要一段时间或是否确实有效。
其他问题的答案
How can I use the url value in the label in a conditional statement? I want an action to be preformed if it changed from the original one.
location.textProperty().addListener((observable, oldValue, newValue) -> {
// perform required action.
});
在 JavaFX 的 WebView
中,我正在努力检测 URL 中的变化。
我在 class:
中有这个方法public Object urlchange() {
engine.getLoadWorker().stateProperty().addListener(new ChangeListener<State>() {
@Override
public void changed(ObservableValue ov, State oldState, State newState) {
if (newState == Worker.State.SUCCEEDED) {
return engine.getLocation());
}
}
});
}
我正在尝试将它用于一个名为 loginbrowser 的对象,例如:
System.out.print(loginbrowser.urlchange());
你能看出我做错了什么吗?
(部分)你做错了什么
您在问题中提供的代码甚至无法编译。 ChangeListener 的 changed 方法是一个空函数,它不能 return 任何值。
无论如何,在 Web 视图中加载内容是一个异步过程。如果您想要在加载 Web 视图后获取 Web 视图位置的值,则需要等待加载完成(在 JavaFX 应用程序线程上不可取,因为这会挂起您的应用程序直到加载完成),或者在回调中收到加载完成的通知(这是您的侦听器正在做的事情)。
(大概)你想做什么
将一些 属性 绑定到网络引擎的位置 属性。例如:
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.VBox;
import javafx.scene.web.*;
import javafx.stage.Stage;
public class LocationViewer extends Application {
@Override
public void start(Stage stage) throws Exception {
Label location = new Label();
WebView webView = new WebView();
WebEngine engine = webView.getEngine();
engine.load("http://www.fxexperience.com");
location.textProperty().bind(engine.locationProperty());
Scene scene = new Scene(new VBox(10, location, webView));
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
launch(args);
}
}
以上代码将在 web 视图的位置发生变化时更新位置标签(通过 运行 代码然后单击一些链接来尝试)。如果您希望仅在页面成功加载后更新标签,那么您需要一个基于 WebView 状态的侦听器,例如:
import javafx.application.Application;
import javafx.concurrent.Worker;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.VBox;
import javafx.scene.web.*;
import javafx.stage.Stage;
public class LocationAfterLoadViewer extends Application {
@Override
public void start(Stage stage) throws Exception {
Label location = new Label();
WebView webView = new WebView();
WebEngine engine = webView.getEngine();
engine.load("http://www.fxexperience.com");
engine.getLoadWorker().stateProperty().addListener((observable, oldValue, newValue) -> {
if (Worker.State.SUCCEEDED.equals(newValue)) {
location.setText(engine.getLocation());
}
});
Scene scene = new Scene(new VBox(10, location, webView));
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
launch(args);
}
}
如果您 运行 最后一个程序并单击某些链接,您会注意到它会延迟位置标签的更新,直到您单击的页面完全加载完毕,这与第一个程序相反一旦位置发生变化,就会更新标签,无论加载是否需要一段时间或是否确实有效。
其他问题的答案
How can I use the url value in the label in a conditional statement? I want an action to be preformed if it changed from the original one.
location.textProperty().addListener((observable, oldValue, newValue) -> {
// perform required action.
});