java - How to retrieve components from HTML webview in JavaFX -


currently have webview displaying simple index.html, webview created in fxmldocument.fxml , controlled in fxmldocumentcontroller.java.
if have button inside index.html how can retrieve or access make using java? there sort of action handler such things?

fxmldocuement.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="510.0" prefwidth="794.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="javafxapplication1.fxmldocumentcontroller">     <children>         <button fx:id="button" layouty="471.0" onaction="#handlebuttonaction" text="click me!" />         <label fx:id="label" layoutx="126" layouty="120" minheight="16" minwidth="69" />       <webview id="webview1" fx:id="webview1" layouty="5.0" prefheight="444.0" prefwidth="882.0" />       <label id="lbl1" fx:id="lbl1" layoutx="88.0" layouty="475.0" prefheight="17.0" prefwidth="99.0" text="label" />     </children> </anchorpane> 

fxmldocumentcontroller.java :

package javafxapplication1;  import java.io.file; import java.net.malformedurlexception; import java.net.url; import java.util.resourcebundle; import javafx.event.actionevent; import javafx.fxml.fxml; import javafx.fxml.initializable; import javafx.scene.control.label; import javafx.scene.web.webview;  public class fxmldocumentcontroller implements initializable {      @fxml     private label label;      @fxml     private webview webview1;      @fxml     private label lbl1;      @fxml     private void handlebuttonaction(actionevent event) throws malformedurlexception {         system.out.println("you clicked me!");         label.settext("hello world!");         lbl1.settext("bonjour");          webview1.getengine().load(new file("c:/users/hadhe/desktop/boots/index.html").touri().tourl().tostring());     }      @override     public void initialize(url url, resourcebundle rb) {         // todo     }      } 

the class containing main : javafxapplication1.java:

package javafxapplication1;  import javafx.application.application; import javafx.fxml.fxmlloader; import javafx.scene.parent; import javafx.scene.scene; import javafx.stage.stage;  public class javafxapplication1 extends application {      @override     public void start(stage stage) throws exception {         parent root = fxmlloader.load(getclass().getresource("fxmldocument.fxml"));          scene scene = new scene(root);          stage.setscene(scene);         stage.show();     }      /**      * @param args command line arguments      */     public static void main(string[] args) {         launch(args);     }  } 

the html document in webengine's document property, it's loaded in background, must wait load:

webview1.getengine().documentproperty().addlistener((o, old, doc) -> listentobutton(doc)); webview1.getengine().load(new file("c:/users/hadhe/desktop/boots/index.html").touri().tourl().tostring()); 

the document object regular xml document, if button has id attribute, can retrieve it:

private void listentobutton(document doc) {     if (doc == null) {         return;     }      string id = "app-action-button";     element button = doc.getelementbyid(id);      // ... } 

if button doesn't have id, can search using xpath:

private void listentobutton(document doc) {     if (doc == null) {         return;     }      xpath xpath = xpathfactory.newinstance().newxpath();     element button = (element)         xpath.evaluate("//input[type='button']", doc,             xpathconstants.node);      // ... } 

finally, can add dom event listener button, described in webengine documentation:

((eventtarget) button).addeventlistener("click", e -> dosomeaction(), false); 

Comments

Popular posts from this blog

java - pagination of xlsx file to XSSFworkbook using apache POI -

Unlimited choices in BASH case statement -

apache - How do I stop my index.php being run twice for every user -