首页 > 解决方案 > 隐藏选项卡上的内容的 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 的图像,但没有它们的内容。

在此处输入图像描述

只有在我至少激活了一次他们的标签后,内容才会显示出来。

在此处输入图像描述

我知道节点需要成为场景的一部分才能拍摄快照,但显然还有更多内容,如果它们是非活动选项卡的一部分,则仅在显示选项卡时才会发生一些渲染步骤。有没有办法强制执行此操作而不需要每个选项卡都处于活动状态?

标签: javafx

解决方案


推荐阅读