Ich versuche, eine Web-Ansicht zu erstellen und Inhalte hinzuzufügen. Wird Inhalte an die Web-Ansicht bei Klick auf eine Schaltfläche anhängen. Hier muss ich die Web-Ansicht programmatisch zu einer gewünschten Position scrollen (z. B. (0, 60)). Ich habe versucht, JavaScript und setValue der ScrollBar Klasse zu verwenden. Aber nichts funktioniert. Hier ist das Beispiel,JavaFx WebView - Scrollen Sie zur gewünschten Position
public class FxWebViewSample extends Application {
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) throws Exception {
Group root = new Group();
final WebView wView = new WebView();
wView.setId("my_view");
wView.setPrefHeight(200);
wView.setPrefWidth(200);
final WebEngine engine = wView.getEngine();
engine.loadContent("<body contentEditable='true'><div id='content'>Initial Text</div><div id='first'>My first web view in fx</div></body><span id='second'>My first web view in fx</span><span id='second'>My first web view in fx</span><span id='second'>My first web view in fx</span><span id='second'>My first web view in fx</span><span id='second'>My first web view in fx</span><span id='second'>My first web view in fx</span><span id='second'>My first web view in fx</span><span id='second'>My first web view in fx</span><div id='first'>My first web view in fx</div></body></body>");
Button appendbtn = new Button("Append Text to Web View");
TextArea area = new TextArea();
area.setText("My text area");
VBox vBox = new VBox();
vBox.getChildren().addAll(wView, appendbtn);
root.getChildren().add(vBox);
Scene scene = new Scene(root, 300, 300);
primaryStage.setScene(scene);
primaryStage.show();
appendbtn.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent arg0) {
/** To append html and text contents to web view */
String webViewContents = (String) engine
.executeScript("document.documentElement.outerHTML");
String appendContent = "<div>Appended html content</div> Appended text content";
engine.loadContent(webViewContents + appendContent);
/**JavaScript to scroll the view*/
//String scrollJsFtn = "var elem = document.getElementById('my_view');var x = 0;var y = 0;while (elem != null) {x += elem.offsetLeft;y += elem.offsetTop;elem = elem.offsetParent;}window.scrollTo(30, 30);";
//engine.executeScript(scrollJsFtn);
Set<Node> scrollBars = wView.lookupAll(".scroll-bar");
for (Node node : scrollBars) {
if (node instanceof ScrollBar) {
ScrollBar sBar = (ScrollBar) node;
sBar.setValue(100.0);
}
}
}
});
}
}
Gibt es eine Möglichkeit, die Bildlaufleiste Position der Webansicht programmgesteuert festzulegen?