JavaFx实现WebView 使用js调用java代码

JavaFx实现WebView 使用js调用java代码:场景是调用java代码打开文件夹

  • html


    
    Show
    
    
    


2019-10-10 18:08:23

666666

2019-10-10 18:08:23

我是文件名
我是文件大小
  • java方法
package com.mandela;

import javafx.application.Application;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.concurrent.Worker;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.web.WebEngine;
import javafx.scene.web.WebView;
import javafx.stage.Stage;
import java.io.File;
import java.io.IOException;
import netscape.javascript.JSObject;

public class JavaTest extends Application {
    @Override
    public void start(Stage primaryStage) throws Exception {
        WebView webView = new WebView();
        WebEngine webEngine = webView.getEngine();
        File directory = new File("");
        String dir=directory.getAbsoluteFile()+"\\show\\show.html";
        String absolutePath = dir;
        System.out.println("xxx"+absolutePath);

        System.out.println("initializeHtml "+absolutePath);
        absolutePath = absolutePath.replace("\\", "/");
        if (absolutePath.startsWith("/")) {
            webEngine.load("file:" + absolutePath);
        } else {
            webEngine.load("file:/" + absolutePath);
        }
        webView.getEngine().getLoadWorker().stateProperty().addListener(new ChangeListener() {
            @Override
            public void changed(ObservableValue ov, Worker.State oldState, Worker.State newState) {
                if (newState == Worker.State.SUCCEEDED) {
                    //System.out.println("123");
                    JSObject win = (JSObject) webEngine.executeScript("window"); // 获取js对象
                    win.setMember("stone", new HelloWorld()); // 然后把应用程序对象设置成为js对象
                    //webEngine.executeScript("changeText()");
                    // 直接执行html中的js脚本
                }
            }
        });
//        webEngine.getLoadWorker().stateProperty()
//                .addListener((obs, oldValue, newValue) -> {
//                    if (newValue == Worker.State.SUCCEEDED) {
//                        webEngine.executeScript("changeText()");
//                        JSObject jsobj = (JSObject) webEngine.executeScript("window");
//                        jsobj.setMember("stone", new HelloWorld());
//                    }
//                });

        //webEngine.load("http://www.www.w3cschool.cn");
        Group root = new Group();
        root.getChildren().add(webView);
        Scene scene = new Scene(root, 600, 500);

        primaryStage.setScene(scene);
        primaryStage.show();
    }



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

    public static class HelloWorld {
        public void openDir() {
            System.out.println("打开目录");
            File directory = new File("");
            String dir=directory.getAbsolutePath();
            System.out.println("打开的文件夹目录:"+dir);
            try {

                Runtime.getRuntime().exec("cmd  /c  explorer " +dir);
            }  catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

你可能感兴趣的:(JavaFx实现WebView 使用js调用java代码)