如何在WebView中自动滚动到结尾?

时间:2013-10-23 15:04:29

标签: javafx-2 javafx

我在WebView中使用Scrollbar有问题。我想在加载Web视图时,srollbar自动滚动到Web视图的结尾。 我该怎么办?

这是我的样本

package sample;  
import javafx.fxml.FXML;  
import javafx.fxml.Initializable;  
import javafx.scene.Node;  
import javafx.scene.control.ScrollBar;  
import javafx.scene.web.WebEngine;  
import javafx.scene.web.WebView;  
import java.net.URL;  
import java.util.ResourceBundle;  
import java.util.Set;  
public class Controller implements Initializable {  
    @FXML  
    WebView webView;  
    @Override  
    public void initialize(URL url, ResourceBundle resourceBundle) {  
        loadWebView();  
    }  
    private void loadWebView() {  
        webView.setStyle("-fx-background-color: #cccccc; -fx-border-color: #00a7c8");  
        StringBuilder html= new StringBuilder().append("<html><body>");  
        for (int i=0; i<100; i++) {  
            html.append("<h1>" + i + ". AAAAAAAAAAAA</h1></br>");  
        }  
        html.append("</body></html>");  
        Set<Node> nodes = webView.lookupAll(".scroll-bar");  
        for (Node node : nodes) {  
            if (ScrollBar.class.isInstance(node)) {  
                System.out.println("Scrollbar here!!!");  
                ScrollBar scroll = (ScrollBar) node;  
                scroll.setValue(scroll.getMax());  
            }  
        }  
        WebEngine webEngine = webView.getEngine();  
        webEngine.loadContent(html.toString());  
    }  
}  

感谢您的阅读

3 个答案:

答案 0 :(得分:2)

只需在loadContent调用后执行此脚本:

webEngine.executeScript("window.scrollTo(0, document.body.scrollHeight);");

答案 1 :(得分:1)

来自https://forums.oracle.com/message/11243184#11243184

package sample;  


import com.sun.javafx.scene.control.skin.ScrollBarSkin;  
import com.sun.webpane.sg.theme.ScrollBarThemeImpl;  
import javafx.beans.value.ChangeListener;  
import javafx.beans.value.ObservableValue;  
import javafx.collections.ListChangeListener;  
import javafx.event.EventHandler;  
import javafx.fxml.FXML;  
import javafx.fxml.Initializable;  
import javafx.geometry.Orientation;  
import javafx.scene.Node;  
import javafx.scene.control.ScrollBar;  
import javafx.scene.control.TextArea;  
import javafx.scene.web.WebEngine;  
import javafx.scene.web.WebEvent;  
import javafx.scene.web.WebView;  


import java.net.URL;  
import java.util.ResourceBundle;  
import java.util.Set;  


public class Controller implements Initializable {  
    @FXML  
    WebView webView;  


    @Override  
    public void initialize(URL url, ResourceBundle resourceBundle) {  
        loadWebView();  
    }  


    private void loadWebView() {  
        webView.setStyle("-fx-background-color: #cccccc; -fx-border-color: #00a7c8");  
        StringBuilder html= new StringBuilder().append("<html>");  
        html.append("<head>");  
        html.append("   <script language=\"javascript\" type=\"text/javascript\">");  
        html.append("       function toBottom(){");  
        html.append("           window.scrollTo(0, document.body.scrollHeight);");  
        html.append("       }");  
        html.append("   </script>");  
        html.append("</head>");  
        html.append("<body onload='toBottom()'>");  


        for (int i=0; i<100; i++) {  
            html.append("<h1>" + i + ". AAAAAAAAAAAA</h1></br>");  
        }  


        html.append("</body></html>");  


        WebEngine webEngine = webView.getEngine();  
        webEngine.loadContent(html.toString());  
    }  
}  

答案 2 :(得分:0)

您必须等到d0完成后才能继续向下滚动。

Worker

});