如何使用platform.runlater()在一个webview中禁用链接/超链接/导航,并在java中的另一个webview中打开该链接/超链接/导航

时间:2017-08-29 05:59:05

标签: java jquery javafx webview javafx-webengine

[作为参考我附上了我的应用程序截图,如果你看到打开的页面也是相同的,也是同样的,这就是为什么我必须停止上面webview的导航所以它只能打开一个] [1 ]

 public class HV extends Application
    {
        @Override
        public void start(Stage primaryStage) throws Exception
        {                   
            StackPane root = new StackPane();

        HBox hbox = new HBox(30); // create a HBox to hold 2 vboxes        

        // create a vbox with a textarea that grows vertically
        VBox vbox = new VBox(10);  
        final WebView img = new WebView();
          final WebEngine webEngineImg = img.getEngine();     
        vbox.getChildren().addAll(img);

        // create a vbox that grows horizontally inside the hbox
        VBox vbox2 = new VBox();        
        final WebView browser = new WebView();
       final WebEngine webEngine = browser.getEngine();

         final WebView browser1 = new WebView();
       final WebEngine webEngine1 = browser1.getEngine();
          webEngine1.getLoadWorker().stateProperty()
        .addListener(new ChangeListener<Worker.State>() {
          @Override
          public void changed(ObservableValue ov, Worker.State oldState, Worker.State newState) {
            if (newState == Worker.State.SUCCEEDED) {
             primaryStage.setTitle(webEngine1.getLocation());
              String trgurl=webEngine1.getLocation();
              System.out.println(trgurl);


              if(trgurl.matches("(.*)img=(.*)")){

                  int n = trgurl.indexOf("img=");
                  //String str1 = Integer.toString(n);
                  System.out.println(n+4);
                  int len = trgurl.length();
                  System.out.println("string length is: "+trgurl.length());
                  System.out.println(trgurl.substring(n+4,len));
                  String find = "file:"+trgurl.substring(n+4,len);
                  System.out.println(find);

                  webEngineImg.load(find);
                  //webEngine1.load(find);// this is loading image

                  if (trgurl.matches("(.*)target=wb2(.*)")){
                      //int n1= trgurl.indexOf("target=");
                      //String str1 = Integer.toString(n);
                    //  System.out.println(n1+7);
                    //  System.out.println(trgurl.substring(n1+7,98));
                      boolean cancel();
                     webEngine.load("url");

                       }  
              }
             }
          }

        });

        webEngine1.load("url");
       // String url = "https://eclipse.org";
                // Load a page from remote url.
              //  webEngine.load(url);

        vbox2.setPadding(new Insets(3));
        vbox2.setSpacing(3);
         vbox2.getChildren().addAll( browser1,browser);

        HBox.setHgrow(vbox2, Priority.ALWAYS);


        hbox.setPadding(new Insets(20));
//        StackPane.setMargin(hbox, new Insets(20));

        hbox.getChildren().addAll(vbox, vbox2);
        root.getChildren().add(hbox);
        Scene scene = new Scene(root, 500, 300); // the stack pane is the root node


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

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

    }
} 

用于重新装配 - 这是我的代码,其中有三个webview在webview1完整网站打开,在webviewimg只有webview1的动作打开的图像和webview我想加载一个编辑网址,但为此我必须禁用webview1中该链接的导航,以便我可以在webview中打开它,所以我尝试使用platform.runlater在其他webview之后运行该链接,但我得到的错误是状态无法更改为字符串这是我试图添加的一段代码

webEngine1.getLoadWorker().stateProperty()
       .addListener(new ChangeListener<Worker.State>() {
          @Override
          public void changed(ObservableValue ov, Worker.State oldState, Worker.State newState) {
            if (newState == Worker.State.SUCCEEDED) {
             primaryStage.setTitle(webEngine1.getLocation());
              String trgurl=webEngine1.getLocation();
              System.out.println(trgurl);
            if ((address.getQuery() + "").contains("target=true"))
            {
                Platform.runLater(() -> {
                    grid_layout.getChildren().remove(webEngine1);
                    grid_layout.add(webEngine1, 0, 1);
                    webEngine1.load(oldState);

或如何在java中插入jquery以防止导航代码在这里

document.addEventListener('click', function(e) {
    e.stopPropagation();
 }, true);

1 个答案:

答案 0 :(得分:0)

问题解决了。这是代码:

public class HV extends Application
{

@Override
public void start(Stage primaryStage) throws Exception
{   

    StackPane root = new StackPane();

    HBox hbox = new HBox(30); // create a HBox to hold 2 vboxes        

    // create a vbox with a textarea that grows vertically
    VBox vbox = new VBox(10);  
    final WebView img = new WebView();
      final WebEngine Img = img.getEngine();     
    vbox.getChildren().addAll(img);

    // create a vbox that grows horizontally inside the hbox
    VBox vbox2 = new VBox();        
    final WebView browser = new WebView();
   final WebEngine wb2 = browser.getEngine();

     final WebView browser1 = new WebView();
   final WebEngine wb1 = browser1.getEngine();
      wb1.getLoadWorker().stateProperty()
   .addListener(new ChangeListener<Worker.State>() {
      @Override
      public void changed(ObservableValue ov, Worker.State oldState, Worker.State newState) {
                Platform.runLater(() -> {
                    final WebEngine wb1 = browser1.getEngine();
                    if (wb1.getLocation().contains("img")) {
                        System.out.println("BrowserPane.ChangeListener cancelling " + wb1.getLocation());
                        wb1.getLoadWorker().cancel();
                        //  if (newState == Worker.State.SCHEDULED) {
                        primaryStage.setTitle(wb1.getLocation());
                        String trgurl = wb1.getLocation();
                        System.out.println(trgurl);
                           if(trgurl.matches("(.*)img=(.*)")){
              int n = trgurl.indexOf("img=");
              //String str1 = Integer.toString(n);
              System.out.println(n+4);
              int len = trgurl.length();
              System.out.println("string length is: "+trgurl.length());
              System.out.println(trgurl.substring(n+4,len));
              String find = "file:"+trgurl.substring(n+4,len);
              System.out.println(find);

              Img.load(find);
                        //webEngine1.load(find);// this is loading image
                        if (trgurl.matches("(.*)target=wb2(.*)")){
                            int n1= trgurl.indexOf("target=wb2");
                            String str1 = Integer.toString(n1+10);
                            System.out.println(n1+10);
                            System.out.println(trgurl.substring(0,n1+10));
                            String find1= ""+trgurl.substring(0,n1+10);
                            System.out.println(find1);
                            // boolean cancel();
                            wb2.load(find1);
                        }
                        }
                        }  
                        });
                        } 
                        });






    wb1.load("url");
   // String url = "https://eclipse.org";
            // Load a page from remote url.
          //  webEngine.load(url);

    vbox2.setPadding(new Insets(3));
    vbox2.setSpacing(3);
     vbox2.getChildren().addAll( browser1,browser);

    HBox.setHgrow(vbox2, Priority.ALWAYS);


    hbox.setPadding(new Insets(20));
//        StackPane.setMargin(hbox, new Insets(20));

    hbox.getChildren().addAll(vbox, vbox2);
    root.getChildren().add(hbox);
    Scene scene = new Scene(root, 500, 300); // the stack pane is the root node


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

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

}
}

使用platform.runlater()并在侦听器通过getloadworker.cancel()取消loadworker之后