如何通过单击其他阶段的按钮在WebView中加载网页? [英] How to load a web page in a WebView by clicking on a button in other stage?

查看:165
本文介绍了如何通过单击其他阶段的按钮在WebView中加载网页?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用JavaFX浏览器我想在中加载网页FXMLFile1 包含 WebView 通过点击 FXMLFile2 中的按钮来显示 FXMLFile1 我尝试了这段代码:

i'm working on a browser with JavaFX i want to load a web page in FXMLFile1 contains WebView just by clicking on Button in FXMLFile2 to show the page in FXMLFile1 i tried this code :

@FXML
 public void tabfirst (ActionEvent ee) throws IOException { //for the FXMLFile2's button text.


            Socket socket = new Socket();
    try {

        //open cursor
        panoo.setCursor(Cursor.WAIT);
        que.setCursor(Cursor.WAIT);
        bbb.setCursor(Cursor.WAIT);

        //do work

        WebEngine myWebEngine = web1.getEngine(); //this web view is in FXMLFile1
        myWebEngine.load("https://www.google.com");



    }
   catch (IOException e){
       final  Stage stg = new Stage();           
        stg.initModality(Modality.APPLICATION_MODAL);
        stg.initOwner(stg);
        stg.setTitle("Cannot connect to the internet /n Please Verify your connection internet");
        labelno.setText("Cannot connect to the internet...");

       //set cursor
         ancpa.setCursor(Cursor.DEFAULT);

   } finally{
       try{ socket.close(); } catch (Exception e){ }
       }

}

注意此类 tabfirst 位于 FXMLFile2 按钮 >和两个FXML文件在同一个控制器中。
所以,任何机构都可以告诉我我的代码有什么问题,并提前致谢!

note this class tabfirst is in the Button in the FXMLFile2 and the two FXMLfiles are in the same controller. so please can any body show me what's wrong with my code and thanks in advance!

推荐答案

我不知道认为你正在设置web1。我认为,因为2个FXML文件使用相同的控制器(编辑:我的坏,我误读/误解)你期望他们自动共享变量,他们不会!

I don't think you are setting web1. I think that because the 2 FXML files are using the same controller ( my bad, I misread/misunderstood) you are expecting that they are automatically sharing variables, which they do not !

当FXMLLoader加载FXML文件时,每次都会创建一个 new 控制器实例。因此FXMLfile1中的控制器实例不知道FXMLfile2的控制器实例或其任何变量。

When the FXMLLoader loads an FXML file it creates a new instance of the controller every time. So the controller instance from FXMLfile1 doesn't know about the controller instance of FXMLfile2 or any of its variables.

控制器之间共享信息的方式大致有5种: / p>

There are about 5 different ways of sharing information between controllers:


  1. 最简单的就是在两者之间使用getter和setter。

  2. 同样相当简单的是绑定两者之间的属性。

  3. 您可以设置监听器,然后通知他们更改。

  4. 使用消息总线。

  5. 使用注射。

  1. The simplest is just using getters and setters between the two.
  2. Also fairly simple is to bind properties between the two.
  3. You could setup listeners and then notify them of changes.
  4. Use a messaging bus.
  5. Use injection.

您使用哪一个取决于各种因素,需要更多信息来确定您要完成的工作?

Which one you use depends on various factors and requires more information as to what you are trying to accomplish ?

选项1&的基本概要2看起来像这样:

A basic outline of options 1 & 2 would look something like this:

FXMLLoader  fxml1 = .....
fxml1.load();
ctrl1 = fxml1.getController();

FXMLLoader  fxml2 = .....
fxml2.load();
ctrl2 = fxml2.getController();

ctrl2.set????( ctrl1.get????() );  // get something from the one and set it in the other
// if the value in ctrl1 changes it does not necessarily change in ctrl2

ctrl2.property????().bind( ctrl1.property???? );  // ctrl2 binds to a property in ctrl1
// if the value of the ctrl1 property changes it WILL also change in ctrl2

这篇关于如何通过单击其他阶段的按钮在WebView中加载网页?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆