如何将 javafx - tableview 大小修复为当前窗口大小

Rak*_*dam 7 javafx tableview autoresize

我对独立应用程序完全陌生。请任何人帮助我。

我有 6 列的 TableView,其中一半显示在窗口中,如下所示。

在此处输入图片说明

我想修复它当前的窗口大小,即使窗口被展开,tableview 也应该自动调整大小。有没有办法做到这一点?

这是代码片段

GridPane tableGrid= new GridPane();
tableGrid.setVgap(10);
tableGrid.setHgap(10);
Label schoolnameL= new Label(SCHOOL+school_id);
schoolnameL.setId("schoolLabel");
Button exportDataSheetBtn= new Button("Export In File");
tableView.setMaxWidth(Region.USE_PREF_SIZE);
tableView.setColumnResizePolicy(TableView.CONSTRAINED_RESIZE_POLICY);
tableGrid.getChildren().addAll(schoolnameL,exportDataSheetBtn,tableView);
Run Code Online (Sandbox Code Playgroud)

Jon*_*cka 10

这可以通过将首选高度和宽度绑定到初级阶段的高度和宽度来完成。这是一个MCVE

import javafx.application.Application;
import javafx.collections.ObservableList;
import javafx.scene.Scene;
import javafx.scene.control.TableView;
import javafx.stage.Stage;

public class MCVE extends Application {

    @Override
    public void start(Stage stage) {

        TableView<ObservableList<String>> table = new TableView<ObservableList<String>>();

        // We bind the prefHeight- and prefWidthProperty to the height and width of the stage.
        table.prefHeightProperty().bind(stage.heightProperty());
        table.prefWidthProperty().bind(stage.widthProperty());

        stage.setScene(new Scene(table, 400, 400));
        stage.show();
    }

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

}
Run Code Online (Sandbox Code Playgroud)


S.R*_*shi 5

在 GridPane 中创建 Tabel,并在 AnchorPane 中使用 GridPane。单击 GridPane(capture1)。 在此输入图像描述 单击 GridPane 布局 (captur2) 内的 Ancho Pane Constraint。 在此输入图像描述

在表视图布局中选择 Hgrow 和 Vgrow 作为“始终”(capture3)在此输入图像描述