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

标签 java javafx size snapshot

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

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

尺寸改变是个问题,但初级阶段不随其包含的Scene改变尺寸也是个问题。

我曾尝试创建一个 MCVE 来重现该问题,但在尝试这样做几天后,我仍然无法重现该问题。原始程序包含大约 2000 行代码,我不想在这里发布。

为什么 Scene.doCSSLayoutSyncForSnapshot(Node node) 会妥协我的布局,当它首先被正确布局时?我能否以某种方式确保布局在调用此方法之前已正确同步以确保它不会更改任何内容?

最佳答案

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

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

app sketch

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

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

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

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

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

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

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);
        }
    }
}

关于java - Node.snapshot(null, null) 改变场景的大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37318953/

相关文章:

Java: token "}"上存在语法错误?

java - 什么时候更喜欢 Hadoop MapReduce 而不是 Spark?

java - 如何在JavaFX TextArea中显示日志?

java - 选中复选框时启用文本框

java - Dao复杂逻辑sql

java - Java对象的实例大小在不同的JVM中是不同的

java - 使用 Mockito 将 Class<T> 作为参数的方法 stub

size - 打开 ACS/Project 打开日志文件大小增加太多(日志大小巨大)

size - 通过 jmx 监控获取以 MB 或 KB 为单位的 Hazelcast map 大小

java - Neo4j java驱动线程数管理