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

标签 javafx tableview autoresize

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

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

enter image description here

我想修复它当前的窗口大小,即使窗口被展开,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);

最佳答案

这可以通过将首选高度和宽度绑定(bind)到初级阶段的高度和宽度来完成。这是 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();
    }

}

关于javafx - 如何将 javafx - tableview 大小修复为当前窗口大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38912047/

相关文章:

java - 如何针对 gtk 3 库运行 javaFx 应用程序?

java - 带有需要 java.time.Duration 的构造函数的自定义 JavaFX 组件

android - JavaFX 文本流忽略分配的背景颜色和大小

ios - xcode 7 不在模拟器的解析 TableView 中显示数据,但在设备中有效

android - 在钛中调整 View 内所有对象的大小

javafx - 是否可以在 ZuluFX for Mac 上使用 javapackager

java - TablewView setAll 的支持列表重置选择但不重置焦点

xamarin.ios - MonoTouch 和 Storyboard : how to come from table cell to details view

javascript - 当屏幕调整为较小的屏幕时,如何自动调整按钮的大小?

c# - Winforms DataGridView在VirtualMode什么时候调用AutoResizeColumn?