Javafx 2 使用Javascript回调方法更改JavaFX标签

Javafx 2 使用Javascript回调方法更改JavaFX标签,javafx-2,javafx-8,javafx-webengine,Javafx 2,Javafx 8,Javafx Webengine,这个问题可以看作是对我有一个带有标签和WebView的简单应用程序的简单扩展。WebView包含一个小矩形,onclick应该调用JavaFX中的方法并更改标签的文本 下面是我的FXML文件 <?xml version="1.0" encoding="UTF-8"?> <?import javafx.scene.web.*?> <?import java.lang.*?> <?import java.util.*?> <?import ja

这个问题可以看作是对我有一个带有标签和WebView的简单应用程序的简单扩展。WebView包含一个小矩形,onclick应该调用JavaFX中的方法并更改标签的文本

下面是我的FXML文件

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.web.*?>
<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>

<AnchorPane id="AnchorPane" prefHeight="200" prefWidth="320" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8" fx:controller="webviewlabel.FXMLDocumentController">
   <children>
      <VBox prefHeight="200.0" prefWidth="100.0">
         <children>
            <Label id="lblSample" fx:id="lblSample" text="Sample Label" />
            <WebView fx:id="wvSample" prefHeight="200.0" prefWidth="200.0" />
         </children>
      </VBox>
   </children>
</AnchorPane>
在上面的连接器类中,如何获取FXMLController类的处理程序,以便可以访问setLabelText


从对的回答中,我可以理解FXMLDocumentController可以作为参数传递,但我不确定在通过javascript回调访问控制器时如何访问它。

连接器
类中定义一个字段和一个构造函数来初始化它:

public class Connector {

    private final FXMLDocumentController controller ;

    public Connector(FXMLDocumentController controller) {
        this.controller = controller ;
    }

    // ...
}
然后,您可以在
连接()方法中直接引用
控制器

public void connecting() {
    // ...

    controller.setLabelText("Bye World");
    // ...
}
就像你以前想做的那样

现在,您只需参照控制器构造
连接器

public class FXMLDocumentController {

    // ...

    public void initiateWeb() {
        // ...

        webEngine.getLoadWorker().stateProperty().addListener(
            new ChangeListener<State>() {
                // ...

                win.setMember("javaObj", new Connector(FXMLDocumentController.this));
                // ...
            }
        );
    }
    // ...
}  
然后用

webEngine.getLoadWorker().stateProperty().addListener((obs, oldState, newState) -> {
    if (newState == Worker.State.SUCCEEDED) {
        JSObject win = (JSObject) webEngine.executeScript("window");
        win.setMember("javaObj", new Connector(this::setLabelText));      
        System.out.println("FXMLDocumentController.initialize(): Called");
    }
});
public class FXMLDocumentController {

    // ...

    public void initiateWeb() {
        // ...

        webEngine.getLoadWorker().stateProperty().addListener(
            new ChangeListener<State>() {
                // ...

                win.setMember("javaObj", new Connector(FXMLDocumentController.this));
                // ...
            }
        );
    }
    // ...
}  
public class Connector {

    private final Consumer<String> messageUpdater ;

    public Connector(Consumer<String> messageUpdater) {
        this.messageUpdater = messageUpdater ;
    }

    // ...

    public void connecting() {
        // ...
        messageUpdater.accept("Bye world");
        // ...
    }
}
webEngine.getLoadWorker().stateProperty().addListener((obs, oldState, newState) -> {
    if (newState == Worker.State.SUCCEEDED) {
        JSObject win = (JSObject) webEngine.executeScript("window");
        win.setMember("javaObj", new Connector(this::setLabelText));      
        System.out.println("FXMLDocumentController.initialize(): Called");
    }
});