javafx - 隐藏选项卡上的内容的 JavaFx 快照
问题描述
我有一个带有 TabPane 的应用程序。我想创建所有选项卡的快照。有人会认为 tab.getContent().snapshot(new SnapshotParameters(), null) 会起作用。但只有在标签之前处于活动状态时才会出现这种情况。如果不是,则根据其内容会产生奇怪的效果和遗漏。例如,对于 HTMLEditor,我得到了编辑器的基本 UI,但没有包含文本。
示例代码:
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.SnapshotParameters;
import javafx.scene.control.Button;
import javafx.scene.control.Tab;
import javafx.scene.control.TabPane;
import javafx.scene.image.ImageView;
import javafx.scene.image.WritableImage;
import javafx.scene.layout.VBox;
import javafx.scene.web.HTMLEditor;
import javafx.stage.Stage;
public class SnapshotTest extends Application {
// Create a tab with an html editor
public Tab createEditorTab(String txt) {
Tab t = new Tab(txt);
HTMLEditor e = new HTMLEditor();
e.setHtmlText(txt);
t.setContent(e);
return t;
}
@Override
public void start(Stage primaryStage) {
TabPane pane = new TabPane();
TabPane imagePane = new TabPane();
Tab snapshotTab = new Tab("Snapshots");
Button b = new Button("Take Snapshots");
b.setOnAction((e)-> {
imagePane.getTabs().clear();
for (int i=1;i<pane.getTabs().size();i++) {
Tab t = pane.getTabs().get(i);
WritableImage imageView = t.getContent().snapshot(new SnapshotParameters(), null);
Tab imageTab = new Tab("Snapshot of "+t.getText());
ImageView v = new ImageView();
v.setImage(imageView);
imageTab.setContent(v);
imagePane.getTabs().add(imageTab);
}
});
VBox box = new VBox();
box.getChildren().addAll(b,imagePane);
snapshotTab.setContent(box);
pane.getTabs().addAll(snapshotTab,
createEditorTab("Tab 1"),
createEditorTab("Tab 2"));
VBox root = new VBox();
root.getChildren().addAll(pane);
root.setMinSize(300, 400);
Scene scene = new Scene(root, 500, 500);
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
当我点击“拍摄快照”时,我得到了 HTMLEditor 的图像,但没有它们的内容。
只有在我至少激活了一次他们的标签后,内容才会显示出来。
我知道节点需要成为场景的一部分才能拍摄快照,但显然还有更多内容,如果它们是非活动选项卡的一部分,则仅在显示选项卡时才会发生一些渲染步骤。有没有办法强制执行此操作而不需要每个选项卡都处于活动状态?
解决方案
推荐阅读
- mysql - 如何在同一表中的特定列更改时更新列
- django - Django 存储 [文件上传到 AWS S3]
- material-table - 材料表 PDF 导出 - 奇怪的字符
- php - 你是如何建立你的数据库的?新的一年你需要改变很多吗?
- asp.net-core - 使用 Entity Framework 在生产服务器上迁移
- android - 变量作为类的名称
- c# - System.Private.CoreLib.dll 中的“文件名或扩展名太长”
- ios - 为什么 PHAsset creationDate 与 exif 元数据不同?
- c++ - XTestFakeButtonEvent 在 GNOME 栏上不起作用
- macos - 为 Mac 应用程序安装本机消息传递主机