JavaFx WebView - 滚动到所需位置

标签 java javafx javafx-2 scrollbar

我正在尝试创建一个 WebView 并向其中添加内容。单击按钮将内容附加到 Web View 。在这里,我需要以编程方式将 Web View 滚动到所需的位置,比如 (0, 60)。我尝试使用 JavaScript 并使用 ScrollBar 类的 setValue。但没有任何效果。这是示例,

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);
                    }
                }
            }
        });

    }
}

有没有办法以编程方式设置网页 View 的滚动条位置?

最佳答案

想办法。使用 JavaScript,可以以编程方式滚动 Web View 。

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.setJavaScriptEnabled(true);
        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 id='append'>Appended html content</div> Appended text content";

                StringBuilder scrollHtml = scrollWebView(0, 50);

                engine.loadContent(scrollHtml + webViewContents + appendContent);
            }
        });

    }

    public static StringBuilder scrollWebView(int xPos, int yPos) {
        StringBuilder script = new StringBuilder().append("<html>");
        script.append("<head>");
        script.append("   <script language=\"javascript\" type=\"text/javascript\">");
        script.append("       function toBottom(){");
        script.append("           window.scrollTo(" + xPos + ", " + yPos + ");");
        script.append("       }");
        script.append("   </script>");
        script.append("</head>");
        script.append("<body onload='toBottom()'>");
        return script;
    }
}

关于JavaFx WebView - 滚动到所需位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22778241/

相关文章:

java - XML JAXB 编码未保存到 xml 文件

javafx - ComboBox 的 setButtonCell

java - FXML 设置 TableView 列大小调整策略

javafx 8 兼容性问题 - FXML 静态字段或方法

java - Axis 展开误差

java - 更新mysql中的记录时出现语法错误异常

java - 仅当应用程序处于前台时才会收到 Android 通知

JavaFX - SetText() to TextArea 在程序完成之前不会执行

java - 指示输入字符串的数据类型?

java - 从集合中获取对象