将JavaFX WebView的大小调整为文档正文所需的最小大小 [英] Size JavaFX WebView to the minimum size needed by the document body
问题描述
我正在JavaFX中为我的项目编写自定义对话框。我需要一个变体但快速模式来显示对话框内容。
I'm writing a custom dialog in JavaFX for my project. I need a variant but quick mode for showing the dialog content.
我知道controlsFX;实际上我正在使用它,但我想在WebView中显示内容,因此它更易于控制。
I know about controlsFX; actually I'm using it but I want to show the content in a WebView so it will be more controllable.
问题在于我想要的WebView的大小找到文档正文的高度并将WebView的大小设置为。
The problem is about the size of the WebView I want to find the height of the document body and set the size of the WebView to that.
我尝试了以下javascript hack但在显示对话框之前我得到的大小为0显示对话框后,该值不可靠。是否有更好的解决方案,或者我应该忘记它并尝试嵌入支持HTML 3的swing组件?
I tried the following javascript hack but before showing the dialog I get 0 for the size and after showing dialog the value is not reliable. Is there a better solution or I should forget about it and try to embed a swing component that supports HTML 3?
WebView vw = new WebView();
vw.getEngine().loadContent("<p>This is my text</p><b>Another</b><p> This is a paragraph</p><ul><li>First cause</li><li>Second cause></li></ul>");
vw.setPrefSize(500, 50);
vw.setOnMouseClicked(new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent event) {
// I see that executeScript doesn't work correctly even when the WebView is loaded
Integer h = (Integer) vw.getEngine().executeScript("document.body.offsetHeight");
System.out.println("scriptRES: " + h );
vw.setPrefSize(400, h);
vw.setMinSize(400, h);
vw.setMaxSize(400, h);
}
});
推荐答案
功能请求涵盖WebView的自动调整大小支持在JavaFX问题跟踪器中:
Automated sizing support of WebView is covered by a feature request in the JavaFX issue tracker:
- RT-25005 Automatic preferred sizing of WebView
功能请求目前尚未实现或计划实施。您可以对功能请求进行投票或评论。
The feature request has currently not been implemented or scheduled for implementation. You can vote for or comment on the feature request.
我知道没有解决方法。早期版本的WebView似乎可以使用以下代码 engine.executeScript(document.width)
和 engine.executeScript(document.height )
。但是这些命令在我上次尝试时不再有效。
There is no work-around that I know of. Earlier version of WebView seemed to work with the following code engine.executeScript("document.width")
and engine.executeScript("document.height")
. But these commands no longer worked last time I tried them.
这篇关于将JavaFX WebView的大小调整为文档正文所需的最小大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!