I have this code that simply loads an HTML file to a JavaFX javafx.scene.web.WebView from resources:

import java.awt.Dimension;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.embed.swing.JFXPanel;
import javafx.scene.Scene;
import javafx.scene.layout.StackPane;
import javafx.scene.web.WebView;
import javafx.stage.Stage;
import javax.swing.JFrame;

public class Test extends Application {

    JFXPanel panel;
    JFrame frame;

    @Override
    public void start(Stage primaryStage) {
        Platform.runLater(new Runnable() {
            public void run() {
                frame = new JFrame("Test");

                WebView v = new WebView();
                v.getEngine().load(getClass().getResource("htmlfile.html").toExternalForm());

                StackPane root = new StackPane();
                root.getChildren().add(v);
                Scene scene = new Scene(root);

                panel = new JFXPanel();
                panel.setScene(scene);
                panel.setPreferredSize(new Dimension(641, 725));

                frame.setContentPane(panel);
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setResizable(false);
                frame.setVisible(true);
            }
        });

    }

    /**
     * main method
     *
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        launch(args);
    }
}

htmlfile.html is this:

<html>
    <head>
        <link rel="stylesheet" href="style.css">
    </head>

    <body>
        <textarea id="mytextarea"></textarea>
    </body>
</html>

Everything is displayed correctly, the only problem is that if i type something in the textarea displayed in the WebView it doesn't recognize the ENTER key as a newline character. So in fact what it happens is that when i press ENTER, the textarea does nothing. Now, if you try the HTML above in a browser (like chrome or firefox) it will work.

Why is this happening?

How can i solve it?

有帮助吗?

解决方案

We had this exact same problem at my work in a Swing application using a JavaFX HTMLEditor (which internally uses a WebView).

This answer helped me workaround the problem. As the other answer saids, it's a known bug in JavaFX.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top