【问题标题】:how to fix javafx - tableview size to current window size如何将 javafx - tableview 大小修复为当前窗口大小
【发布时间】:2016-12-19 02:50:48
【问题描述】:

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

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

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

【问题讨论】:

  • 添加一些与 TableView 及其父容器相关的代码将有助于我们更好地帮助您。

标签: javafx tableview autoresize


【解决方案1】:

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

}

【讨论】:

    【解决方案2】:

    您可以使用 ScrollPane 作为场景的根,并将其他所有内容放入其中。然后将属性 setFitToWidth 和 setFitToHeight 设置为 true,并且 ScrollPane 内的所有内容都将被拉伸以适应 ScrollPane 的大小,并且 ScrollPane 将适合场景,因为它是一个布局窗格。如果用户将窗口大小调整为小于内容的 minWidth,它也会显示 ScrollBars,因此内容不会被截断!

    @Override
    public void start(Stage stage) {
    
        TableView<ObservableList<String>> table = new TableView<ObservableList<String>>();
        table.setMinWidth(400);
    
        ScrollPane sp = new ScrollPane(table);
        sp.setFitToHeight(true);
        sp.setFitToWidth(true);
    
        stage.setScene(new Scene(table, 800, 600));
        stage.show();
    }
    
    public static void main(String[] args) {
        launch();
    }
    

    我从乔纳森的回答中复制了部分 MCVE,希望你不要介意乔纳森 :)

    有关制作可调整大小的 GUI 的更多一般提示,请查看post

    【讨论】:

      【解决方案3】:

      在 GridPane 中创建 Tabel 并在 AnchorPane 中使用 GridePane。单击 GridPane(capture1)。 单击 GridPane 布局 (captur2) 内的 Ancho Pane Constraint。

      在表格视图布局中选择 Hgrow 和 Vgrow 为 'Always'(capture3)

      【讨论】:

        【解决方案4】:
        VBox.setVgrow(tableView, Priority.ALWAYS);
        

        或者你的父布局:

         VBox.setVgrow({{PARENT}}, Priority.ALWAYS);
        

        它为我修好了:

        【讨论】:

          猜你喜欢
          • 2014-06-22
          • 2014-07-04
          • 2015-02-13
          • 2023-04-11
          • 1970-01-01
          • 1970-01-01
          • 2013-07-29
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多