Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/349.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
JavaFx WebView-滚动到所需位置_Java_Javafx_Javafx 2_Scrollbar - Fatal编程技术网

JavaFx WebView-滚动到所需位置

JavaFx WebView-滚动到所需位置,java,javafx,javafx-2,scrollbar,Java,Javafx,Javafx 2,Scrollbar,我正在尝试创建一个web视图并向其中添加内容。将在单击按钮时将内容附加到web视图。在这里,我需要通过编程将web视图滚动到所需的位置,例如(0,60)。我尝试使用JavaScript和ScrollBar类的setValue。但一切都没有结果。这是样品 public class FxWebViewSample extends Application { public static void main(String[] args) { launch(args);

我正在尝试创建一个web视图并向其中添加内容。将在单击按钮时将内容附加到web视图。在这里,我需要通过编程将web视图滚动到所需的位置,例如(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);
                    }
                }
            }
        });

    }
}
公共类FxWebViewSample扩展应用程序{
公共静态void main(字符串[]args){
发射(args);
}
@凌驾
public void start(Stage primaryStage)引发异常{
组根=新组();
最终WebView wView=新WebView();
setId(“我的视图”);
wView.setPrefHeight(200);
wView.setPrefWidth(200);
final WebEngine=wView.getEngine();
engine.loadContent(“初始文本fxMy第一个web视图fxMy第一个web视图fxMy第一个web视图fxMy第一个web视图fxMy第一个web视图fxMy第一个web视图fxMy第一个web视图fxMy第一个web视图fxMy第一个web视图fxMy第一个web视图fxMy第一个web视图fxMy第一个web视图”);
按钮appendbtn=新按钮(“将文本追加到Web视图”);
TextArea=新建TextArea();
area.setText(“我的文本区域”);
VBox VBox=新的VBox();
vBox.getChildren().addAll(wView,appendbtn);
root.getChildren().add(vBox);
场景=新场景(根,300,300);
初级阶段。场景(场景);
primaryStage.show();
appendbtn.setOnAction(新的EventHandler(){
@凌驾
公共无效句柄(ActionEvent arg0){
/**将html和文本内容附加到web视图的步骤*/
字符串webViewContents=(字符串)引擎
.executeScript(“document.documentElement.outerHTML”);
String appendContent=“追加的html内容追加的文本内容”;
engine.loadContent(webViewContents+appendContent);
/**JavaScript来滚动视图*/
//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);”;
//engine.executeScript(scrollJsFtn);
设置滚动条=wView.lookupAll(“.scrollbar”);
用于(节点:滚动条){
if(滚动条的节点实例){
滚动条sBar=(滚动条)节点;
sBar设定值(100.0);
}
}
}
});
}
}

有没有一种方法可以通过编程方式设置web视图的滚动条位置?

解决这个问题。使用JavaScript可以通过编程方式滚动web视图

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;
    }
}
公共类FxWebViewSample扩展应用程序{
公共静态void main(字符串[]args){
发射(args);
}
@凌驾
public void start(Stage primaryStage)引发异常{
组根=新组();
最终WebView wView=新WebView();
setId(“我的视图”);
wView.setPrefHeight(200);
wView.setPrefWidth(200);
final WebEngine=wView.getEngine();
engine.setJavaScriptEnabled(true);
engine.loadContent(“初始文本fxMy第一个web视图fxMy第一个web视图fxMy第一个web视图fxMy第一个web视图fxMy第一个web视图fxMy第一个web视图fxMy第一个web视图fxMy第一个web视图fxMy第一个web视图fxMy第一个web视图fxMy第一个web视图fxMy第一个web视图”);
按钮appendbtn=新按钮(“将文本追加到Web视图”);
TextArea=新建TextArea();
area.setText(“我的文本区域”);
VBox VBox=新的VBox();
vBox.getChildren().addAll(wView,appendbtn);
root.getChildren().add(vBox);
场景=新场景(根,300,300);
初级阶段。场景(场景);
primaryStage.show();
appendbtn.setOnAction(新的EventHandler(){
@凌驾
公共无效句柄(ActionEvent arg0){
/**将html和文本内容附加到web视图的步骤*/
字符串webViewContents=(字符串)引擎
.executeScript(“document.documentElement.outerHTML”);
String appendContent=“追加的html内容追加的文本内容”;
StringBuilder scrollHtml=scrollWebView(0,50);
loadContent(scrollHtml+webViewContents+appendContent);
}
});
}
公共静态StringBuilder scrollWebView(int xPos、int YPO){
StringBuilder脚本=新建StringBuilder().append(“”);
脚本。追加(“”);
脚本。追加(“”);
append(“函数toBottom(){”);
script.append(“window.scrollTo”(“+xPos+”,“+yPos+”);”;
script.append(“}”);
脚本。追加(“”);
脚本。追加(“”);
脚本。追加(“”);
返回脚本;
}
}