JavaFX WebView滚动问题

时间:2017-01-03 02:23:27

标签: java javafx webview

我使用JavaFX开发了一个应用程序,该应用程序部分使用了WebView。但是,我注意到,当用户或通过JS以编程方式将WebView滚动到底部或靠近底部时,似乎会发生奇怪的事情。我特别希望以某种方式来阻止这种行为,并且仍然需要允许在两种意义上滚动。它与this问题相似,但它们不一样。我的帖子的其余部分解释了发生了什么。

如果滚动到底部并将窗口调整为比以前大,并将鼠标悬停在窗口中的其他JavaFX控件上,它们将变为白框。

为了说明,假设您有以下窗口:

enter image description here

滚动到底部并将窗口调整为比以前更大,然后将鼠标悬停在JavaFX按钮和TextBox上,当你这样做时它们都会消失:

enter image description here

至少在Windows 10上使用Java8u111和Java8u51。我已经举了一个例子来证明这一点:

example.Main.java

package example;

import java.io.IOException;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;

public class Main extends Application {

    @Override
    public void start(Stage primaryStage) throws IOException {
        FXMLLoader loader = new FXMLLoader();
        loader.setLocation(Main.class.getResource("Example.fxml"));
        BorderPane rootWindow = (BorderPane) loader.load();

        Scene scene = new Scene(rootWindow);
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
}

example.ExampleController.java

package example;

import javafx.concurrent.Worker;
import javafx.fxml.FXML;
import javafx.scene.web.WebEngine;
import javafx.scene.web.WebView;

public class ExampleController {
    @FXML private WebView webView;

    @FXML
    private void initialize() {
        WebEngine engine = webView.getEngine();
        engine.load("https://stackoverflow.com");
        engine.getLoadWorker().stateProperty().addListener( (o, oldVal, newVal) -> {
            if (newVal == Worker.State.SUCCEEDED) {
                engine.executeScript("window.scrollTo(0, document.body.scrollHeight);");
            }
        });
    }
}

example.Example.fxml

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.BorderPane?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.web.WebView?>

<BorderPane prefHeight="400.0" prefWidth="500.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8.0.111" fx:controller="example.ExampleController">
   <center>
      <WebView fx:id="webView" prefHeight="200.0" prefWidth="200.0" BorderPane.alignment="CENTER">
         <BorderPane.margin>
            <Insets bottom="10.0" left="10.0" right="10.0" top="10.0" />
         </BorderPane.margin>
      </WebView>
   </center>
   <bottom>
      <HBox BorderPane.alignment="CENTER">
         <children>
            <TextField HBox.hgrow="ALWAYS" />
            <Button alignment="TOP_LEFT" mnemonicParsing="false" text="Button" />
         </children>
      </HBox>
   </bottom>
</BorderPane>

有没有办法可以阻止这种情况发生或至少减轻它?

1 个答案:

答案 0 :(得分:0)

我也遇到了同样的问题,并且找到了解决方案: 向jvm添加参数: -Dprism.order = sw

请参阅:https://news.kynosarges.org/2016/04/09/javafx-and-java_tool_options/?tdsourcetag=s_pctim_aiomsg

相关问题