2 Replies Latest reply: Oct 23, 2013 11:28 PM by TranQuocUng RSS

    How to auto-scroll  to the end in WebView ?

    TranQuocUng

      Hi All,

       

      I've problem with Scrollbar in WebView. I want to when the web view loaded, the srollbar auto scroll to the end of web view.

      How can I do it?

       

      This is my sample

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

       

      Thanks 4 your reading

        • 1. Re: How to auto-scroll  to the end in WebView ?
          TranQuocUng

          Hi anybody, could you help me?

          • 2. Re: How to auto-scroll  to the end in WebView ?
            TranQuocUng

            Hi all I did it well done. It's just javascripts. This is my sample code:

             

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

             

            Thanks 4 your reading