Javafx 2 在JavaFX2中加载内容后,提交按钮单击不起作用
我正在通过javafxweb引擎加载一个url。我的要求是在页面加载时修改嵌入的HTML,以便通过更改字体源正确加载字体。我能够获取文档对象并转换为字符串,替换相应的部分,然后用webengine.loadContent(string)加载HTML。但是,一旦页面出现并单击submit按钮,就不会发生任何事情。下面是我的代码,所有这些都是通过它实现的:Javafx 2 在JavaFX2中加载内容后,提交按钮单击不起作用,javafx-2,Javafx 2,我正在通过javafxweb引擎加载一个url。我的要求是在页面加载时修改嵌入的HTML,以便通过更改字体源正确加载字体。我能够获取文档对象并转换为字符串,替换相应的部分,然后用webengine.loadContent(string)加载HTML。但是,一旦页面出现并单击submit按钮,就不会发生任何事情。下面是我的代码,所有这些都是通过它实现的: webEngine.getLoadWorker().stateProperty().addListener(new ChangeListen
webEngine.getLoadWorker().stateProperty().addListener(new ChangeListener<Worker.State>() {
String htmlBody = null;
@Override
public void changed(ObservableValue<? extends Worker.State> observableValue, Worker.State prevState, Worker.State newState) {
//To change body of implemented methods use File | Settings | File Templates.
//String htmlBody = null;
int count = 0;
if (newState == Worker.State.SUCCEEDED) {
browser.requestFocus();
// Get the document object from Engine.
Document doc = webEngine.getDocument();
try {
// Use Transformer to convert the HTML Object from the document top String format.
Transformer transformer = TransformerFactory.newInstance().newTransformer();
transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
transformer.setOutputProperty(OutputKeys.METHOD, "html");
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
StringWriter outWriter = new StringWriter();
transformer.transform(new DOMSource(doc),new StreamResult(outWriter));
StringBuffer sb = outWriter.getBuffer();
htmlBody = sb.toString();
// Replace the font-family attribute in the style section to the actual URL of the font being used.
htmlBody = htmlBody.replace("font-family: medium", "font-family: url(http://1.10.30.45:8080/fonts/Md.ttf)");
observableValue.removeListener(this);
// Load the new HTML string to the Engine.
webEngine.loadContent(htmlBody, "text/html");
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
});
webEngine.load(url);
//add the web view to the scene
getChildren().add(browser);
但在这种情况下,实际字体会更改为一些默认字体 按下提交按钮时,HTML/Javascript代码运行的是什么?相关表单的
action
属性是什么?我怀疑由于webEngine.loadContent()
调用,页面不知道在哪里提交表单,这就是它无法工作的原因
通过正常加载页面并使用
WebEngine
的userStyleSheetLocation
属性手动设置样式表(),可以解决样式问题。我建议您尝试一下userStyleSheetLocation
自定义字体加载问题可以通过使用最新版本的javafx的jdk8解决。JDK1.8.0中的版本是javafx.runtime.version=8.0.0
早期的JDK1.7.0_45是javafx.runtime.version=2.2.45
谢谢。我之前尝试过使用userStyleSheetLocation,但无法覆盖已应用于页面的css。然而,字体问题今天在我将jdk 8与最新版本的javaFX一起使用时得到了解决。
// Load the new HTML string to the Engine.
webEngine.loadContent(htmlBody, "text/html");