当用户点击链接时,我想更改页面,然后加载相关信息。
但是,HTMLElement
始终为null
。
以下是代码:
WebViewPanel.java
//to be used in JFX Thread
public void loadURLFX(final String url) {
if (Misc.stringHasContent(url)) {
String tmp = toURL(url);
if (tmp == null) {
tmp = toURL("http://" + url);
}
boolean invalidURL = false;
InputStream stream = null;
try {
URL urlObj = new URL(tmp);
stream = urlObj.openStream();
} catch (Exception e) {
invalidURL = true;
} finally {
try {
stream.close();
} catch (Exception ex) {
}
stream = null;
}
if (!invalidURL) {
engine.load(tmp);
} else {
//https://www.google.com.sg/search?q=searchTerm
engine.load("https://www.google.com.sg/search?q=" + url);
}
}
}
App.java
public void load(){
if(Platform.isFxApplicationThread()){
webViewPanel.loadURLFX(App.class.getResource("/hello.html").toExternalForm());
HTMLElement element = (HTMLElement) WebMisc.getHTMLElement(webViewPanel.getWebEngine(), "menu");
}
}
element
始终为空。我该怎么做才能解决它?