Node.snapshot(null, null) 改变场景的大小

Node.snapshot(null, null) changes size of Scene

我有 Scene,它设置为我的 primaryStage 的 Scene,在其他节点中,它包含 VBoxTableView 以及一些按钮。当我使用 TableRow.snapshot(null, null) 在 table 中的一行上拍摄快照时,Scene 的大小发生了变化。宽度改变了大约 10 像素,而高度改变了大约 40 - 有时超过 600 (!) - 像素。

发生这种情况是因为 Node.snapshot(null, null) 调用 Scene.doCSSLayoutSyncForSnapshot(Node node) 似乎获得了所有节点的首选大小并使用它重新计算大小。这不知何故 returns 错误的值,因为我的节点只指定了首选大小并且在调用此方法之前看起来很棒。有什么办法可以避免这种情况吗?

大小变化是个问题,但初级不随其包含的Scene大小变化也是个问题

我曾尝试创建一个 MCVE 来重现该问题,但经过几天的尝试,我仍然无法重现该问题。原始程序包含大约 2000 行代码,我不想在此处 post。

为什么 Scene.doCSSLayoutSyncForSnapshot(Node node) 一开始就正确布局了我的布局?我能否以某种方式确保在调用此方法之前布局已正确同步以确保它不会更改任何内容?

问题已解决。不得不复制我的整个项目,然后删除部分代码,直到问题消失。

无论如何。我的应用程序基本上包含三个组件。一个导航组件、一个 table 组件和一个状态栏组件。它看起来像这样:

我遇到的问题是,每当我对 table 中的一行进行快照时,状态栏的宽度以及 table 组件的宽度和高度都会增加。

显然,这是由于状态栏组件的填充。它有 5 个像素的左右填充,一旦我删除了填充,问题就消失了。

增加的 10 个像素宽度使包含所有这些的 BorderPane 扩展了相同数量的像素,并且由于 table 宽度绑定到 BorderPane 宽度,它增加了相同的数量。但我仍然不明白的是,为什么包含 BorderPaneStage 没有调整到新的宽度。

组件在Scene.doCSSLayoutSyncForSnapshot(Node node)被调用之前被适当填充,所以我不明白为什么要添加十个像素的额外宽度。

无论如何:从状态栏组件中删除填充,而不是填充状态栏内的组件解决了这个问题。如果有人对此有很好的解释,我洗耳恭听。

这是一个 MCVE,您可以通过在 table 中拖动一行来重现问题:

import java.io.File;
import java.sql.SQLException;
import java.util.ArrayList;

import javafx.application.Application;
import javafx.beans.property.SimpleStringProperty;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.geometry.Insets;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableRow;
import javafx.scene.control.TableView;
import javafx.scene.input.ClipboardContent;
import javafx.scene.input.Dragboard;
import javafx.scene.input.TransferMode;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Region;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class MCVE extends Application {

    private Stage primaryStage;
    private BorderPane rootLayout;
    private VBox detailsView;
    private StatusBar statusBar;

    public void start(Stage primaryStage) throws SQLException {

        this.primaryStage = primaryStage;
        this.primaryStage.setTitle("MCVE");

        initRootLayout();
        showStatusBar();
        showDetailsView();

        detailsView.prefWidthProperty().bind(rootLayout.widthProperty());
        detailsView.prefHeightProperty().bind(rootLayout.heightProperty());
    }

    @Override
    public void init() throws Exception {
        super.init();

    }

    public void initRootLayout() {
        rootLayout = new BorderPane();

        primaryStage.setWidth(1000);
        primaryStage.setHeight(600);

        Scene scene = new Scene(rootLayout);
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    public void showStatusBar() {
        statusBar = new StatusBar();
        rootLayout.setBottom(statusBar);
    }

    public void showDetailsView() {
        detailsView = new VBox();
        rootLayout.setCenter(detailsView);

        setDetailsView(new Table(this));

        detailsView.prefHeightProperty().bind(primaryStage.heightProperty());
        detailsView.setMaxHeight(Region.USE_PREF_SIZE);
    }

    public static void main(String[] args) {
        launch(args);
    }

    public VBox getDetailsView() {
        return detailsView;
    }

    public void setDetailsView(Node content) {
        detailsView.getChildren().add(0, content);
    }

    public StatusBar getStatusBar() {
        return statusBar;
    }

    class StatusBar extends HBox {
        public StatusBar() {
            setPadding(new Insets(0, 5, 0, 5));

            HBox leftBox = new HBox(10);

            getChildren().addAll(leftBox);

            /**
             * CONTROL SIZES
             */
            setPrefHeight(28);
            setMinHeight(28);
            setMaxHeight(28);

            // Leftbox takes all the space not occupied by the helpbox.
            leftBox.prefWidthProperty().bind(widthProperty());

            setStyle("-fx-border-color: black;");
        }
    }

    class Table extends TableView<ObservableList<String>> {

        private ObservableList<ObservableList<String>> data;

        public Table(MCVE app) {

            prefWidthProperty().bind(app.getDetailsView().widthProperty());
            prefHeightProperty()
                    .bind(app.getDetailsView().heightProperty());

            widthProperty().addListener((obs, oldValue, newValue) -> {
                System.out.println("Table width: " + newValue);
            });

            setRowFactory(r -> {
                TableRow<ObservableList<String>> row = new TableRow<ObservableList<String>>();

                row.setOnDragDetected(e -> {
                    Dragboard db = row.startDragAndDrop(TransferMode.ANY);
                    db.setDragView(row.snapshot(null, null));

                    ArrayList<File> files = new ArrayList<File>();

                    // We create a clipboard and put all of the files that
                    // was selected into the clipboard.
                    ClipboardContent filesToCopyClipboard = new ClipboardContent();
                    filesToCopyClipboard.putFiles(files);

                    db.setContent(filesToCopyClipboard);
                });

                row.setOnDragDone(e -> {
                    e.consume();
                });

                return row;
            });

            ObservableList<String> columnNames = FXCollections.observableArrayList("Col1", "col2", "Col3", "Col4");

            data = FXCollections.observableArrayList();

            for (int i = 0; i < columnNames.size(); i++) {
                final int colIndex = i;

                TableColumn<ObservableList<String>, String> column = new TableColumn<ObservableList<String>, String>(
                        columnNames.get(i));

                column.setCellValueFactory((param) -> new SimpleStringProperty(param.getValue().get(colIndex).toString()));

                    getColumns().add(column);
            }

            // Adds all of the data from the rows the data list.
            for (int i = 0; i < 100; i++) {
                // Each column from the row is a String in the list.
                ObservableList<String> row = FXCollections.observableArrayList();

                row.add("Column 1");
                row.add("Column 2");
                row.add("Column 3");
                row.add("Column 4");

                // Adds the row to data.
                data.add(row);
            }

            // Adds all of the rows in data to the table.
            setItems(data);
        }
    }
}