java - How retrieve a String from Webengine back to invoking class after page loading -
i'm java , javafx newbie. don't know how pass variable matched
(browser
) class implements webengine application (myscene
) class. both classes listed below. understand (am right?) should done after variable updated, after page loaded. guess there 2 ways it: forcing myscene
class wait seconds, retrieve method (like public string getmatched()
in browser
(not listed) class or notifying myscene
class variable updated. don't know, how it. has suggestion?
myscene.java:
package mypackage; import javafx.application.application; import javafx.scene.scene; import javafx.stage.stage; import javafx.concurrent.task; public class myscene extends application { private scene scene; private static string address = "https://www.google.com"; @override public void start(stage stage) { // create scene stage.settitle("web view"); browser mybrowser = new browser(address); scene = new scene(mybrowser,750,500); stage.setresizable(false); stage.setscene(scene); stage.show(); } public static void main(string[] args){ launch(); } }
browser.java:
package mypackage; import java.util.regex.matcher; import java.util.regex.pattern; import javafx.application.platform; import javafx.beans.value.changelistener; import javafx.beans.value.observablevalue; import javafx.concurrent.worker.state; import javafx.geometry.hpos; import javafx.geometry.vpos; import javafx.scene.node; import javafx.scene.layout.hbox; import javafx.scene.layout.priority; import javafx.scene.layout.region; import javafx.scene.web.webengine; import javafx.scene.web.webview; import org.w3c.dom.element; import org.w3c.dom.nodelist; class browser extends region { private string matched; final webview browser = new webview(); final webengine webengine = browser.getengine(); public browser(string loadaddress) { string myaddress = loadaddress; //apply styles getstyleclass().add("browser"); // load web page webengine.load(myaddress); //add web view scene getchildren().add(browser); webengine.getloadworker().stateproperty().addlistener( new changelistener<state>() { public void changed(observablevalue ov, state oldstate, state newstate) { if (newstate == state.succeeded) { pattern fbpattern = pattern.compile(" <title id=\"([...]+)\">"); nodelist lst = webengine.getdocument().getelementsbytagname("script"); if (lst != null && lst.getlength() > 0) { (int = 0; < lst.getlength(); i++) { if (lst.item(i).getnodetype() == org.w3c.dom.node.element_node) { element el = (element) lst.item(i); matcher fbmatcher = fbpattern.matcher(el.gettextcontent()); if (fbmatcher.find()) { matched = fbmatcher.group(1); } } } } } } }); } private node createspacer() { region spacer = new region(); hbox.sethgrow(spacer, priority.always); return spacer; } @override protected void layoutchildren() { double w = getwidth(); double h = getheight(); layoutinarea(browser,0,0,w,h,0, hpos.center, vpos.center); } @override protected double computeprefwidth(double height) { return 750; } @override protected double computeprefheight(double width) { return 500; } }
Comments
Post a Comment