使用隧道交互在我的webview上重新加载Html文件

时间:2016-02-17 00:29:21

标签: java javafx webview tunnel

我有一个问题,就是现在重新加载我的webview组件上的html文件。

所以,基本上,当我运行应用程序时,我的html文件在我的程序(位于我本地存储的HTML文件)上运行良好。

但是当我想重新加载html文件时(通过在我的webview上执行reaload操作),我的隧道(我为与另一个javafx组件交互而创建)似乎有问题,而且我的html文档有一个应该有的不同行为......

package Visual.SintaxDirectedEditor;

import javafx.application.Platform;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXMLLoader;
import javafx.geometry.Rectangle2D;
import javafx.scene.Scene;
import javafx.scene.control.MenuItem;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.StackPane;
import javafx.scene.web.WebEngine;
import javafx.scene.web.WebView;
import javafx.stage.Screen;
import javafx.stage.Stage;
import netscape.javascript.JSObject;
import org.fxmisc.richtext.CodeArea;
import org.fxmisc.richtext.LineNumberFactory;

import java.io.File;

public class Main {
 private Scene scene;
 private Stage stage;
 private final String title = "liss | SDE";
 private final double width = 600;
 private final double height = 400;
 private final double cX = 0.00, cY = 0.00;


public Main() throws Exception {

    FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("sde.fxml"));
    AnchorPane page = (AnchorPane) fxmlLoader.load();
    scene = new Scene(page);

    stage = new Stage();
    stage.setScene(scene);
    stage.setTitle(this.title);

    //Maximize the menubar width with parent node
    //MenuBar mb = (MenuBar) scene.lookup("#menuBar");
    //mb.prefWidthProperty().bind(stage.widthProperty());

    //Set minimum width and height of the window
    stage.setMinWidth(width);
    stage.setMinHeight(height);

    //Maximize window
    Screen screen = Screen.getPrimary();
    Rectangle2D bounds = screen.getVisualBounds();

    stage.setX(bounds.getMinX());
    stage.setY(bounds.getMinY());
    stage.setWidth(bounds.getWidth());
    stage.setHeight(bounds.getHeight());

    //Set coordinates of the window
    //stage.setY(cY);
    //stage.setX(cX);


    WebView wv = (WebView) fxmlLoader.getNamespace().get("tree");
    StackPane sp = (StackPane) fxmlLoader.getNamespace().get("code_editor");

    //Add Html file to WebEngine and set the context menu of the HTML file false
    File f = new File("ressources/html/index.html");
    WebEngine we = wv.getEngine();
    we.setJavaScriptEnabled(true);
    we.load(f.toURI().toURL().toString());
    wv.setContextMenuEnabled(false);

    //Add the RichText plugin to JavaFx application
    CodeArea codeArea = new CodeArea();
    codeArea.setParagraphGraphicFactory(LineNumberFactory.get(codeArea));
    codeArea.setEditable(false);
    codeArea.setStyle("-fx-font-size:15;");
    sp.getChildren().add(codeArea);

    //Creating a bridge for WebEngine to Java code application
    JSObject jsobj = (JSObject) we.executeScript("window");
    LissProgram l = new LissProgram(codeArea);
    jsobj.setMember("liss", l);


    //When "close menuitem" is clicked, then it exits the program
    MenuItem closeMenuItem = (MenuItem) fxmlLoader.getNamespace().get("close");
    closeMenuItem.setOnAction(new EventHandler<ActionEvent>() {
        @Override
        public void handle(ActionEvent e) {
            Platform.exit();
        }
    });

    //When the "new menuitem" is clicked, then it must create
    MenuItem newMenuItem = (MenuItem) fxmlLoader.getNamespace().get("new");
    newMenuItem.setOnAction(new EventHandler<ActionEvent>() {
        @Override
        public void handle(ActionEvent e) {
            wv.getEngine().reload();
        }
    });


    stage.show();
 }

}

我的代码出了什么问题?

2 个答案:

答案 0 :(得分:1)

您使用以下代码行注册网桥:

//Creating a bridge for WebEngine to Java code application
JSObject jsobj = (JSObject) we.executeScript("window");
final LissProgram lissProg = new LissProgram(codeArea);
jsobj.setMember("liss", lissProg);

实际发生的是在全局上下文(窗口)上创建JavaScript对象liss。但是,当您重新加载页面时,也会重新加载全局上下文。因此,您必须在重新加载后重新注册liss

//When the "new menuitem" is clicked, then it must create
MenuItem newMenuItem = (MenuItem) fxmlLoader.getNamespace().get("new");
newMenuItem.setOnAction(new EventHandler<ActionEvent>() {
    @Override
    public void handle(ActionEvent e) {
        final WebEngine webEngine = wv.getEngine();
        webEngine .reload();
        webEngine.getLoadWorker().stateProperty().addListener(
        new ChangeListener<State>() {
            public void changed(ObservableValue ov, State oldState, State newState) {
                if (newState == State.SUCCEEDED) {
                    JSObject jsobj = (JSObject) we.executeScript("window");
                    jsobj.setMember("liss", lissProg);
                }
            }
        });

    }
});

答案 1 :(得分:0)

好的,所以我最终通过在webengine中添加一个函数来解决问题!

通过这样做:

//Creating a bridge for WebEngine to Java code application
    we.documentProperty().addListener((observable, oldValue, newValue) -> {
        //Comment about this piece of code: When a user want to create a new liss file, we must add this piece of code into the function addListener function.
        JSObject jsobj = (JSObject) we.executeScript("window");
        LissProgram l = new LissProgram(codeArea);
        jsobj.setMember("liss", l);
    });

我添加了一个&#34; addListener&#34;在那里运行并更改我的JSObject。 现在,我的页面重新加载,隧道/网桥完美运行。 希望它对未来的开发者有所帮助。