JavaFX:设置每列的最小宽度

时间:2019-08-21 09:19:35

标签: java javafx tableview

我正在尝试将每列的最小宽度设置为100px。我更喜欢使用tableView.setColumnResizePolicy(TableView.CONSTRAINED_RESIZE_POLICY);

说明

  

如果您设置TableView.setColumnResizePolicy(TableView.CONSTRAINED_RESIZE_POLICY),则所有列的大小都会相等,直到达到TableViews的最大宽度为止。

Explanation from

此刻,每列的宽度由TableView的宽度决定,该宽度除以列数。

我试图为每一列设置最小宽度,但这没有用。我还看到人们只是为setColumnResizePolicy创建了自己的回调,但是我无法实现应该发生的想法。

MCVE

public class MCVE extends Application {

    private Scene mainScene;
    private Stage mainStage;
    private Label filename;
    private VBox mainBox;

    private TableView<String> tableView;

    private Button open;
    private Button save;
    private Button neu;
    private Button settings;
    private Button table;
    private Button row;
    private Button column;
    private Button date;

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

    @Override
    public void start(Stage primaryStage) throws Exception {

        initButton();
        initTable();

        mainStage = new Stage();
        filename = new Label("Nr. 100 - Test Data (Applikation: TEST)");
        mainScene = new Scene(mainVBox(), 1200, 600);


        tableView.prefWidthProperty().bind(mainBox.widthProperty());
        tableView.prefHeightProperty().bind(mainBox.heightProperty());

        mainStage.setScene(mainScene);
        mainStage.show();
    }

    private GridPane mainGrid() {

        GridPane gridPane = new GridPane();
        gridPane.setVgap(10);
        gridPane.setHgap(10);

        gridPane.add(filename, 0, 0);
        gridPane.add(buttonBox(), 0, 1);
        gridPane.add(tableView, 0, 2);

        return gridPane;
    }

    private VBox buttonBox() {

        VBox buttonBox = new VBox();

        HBox firstRowBox = new HBox();
        HBox secRowBox = new HBox();

        firstRowBox.getChildren().addAll(open, save, neu, settings);
        firstRowBox.setSpacing(5);
        secRowBox.getChildren().addAll(table, row, column, date);
        secRowBox.setSpacing(5);

        buttonBox.getChildren().addAll(firstRowBox, secRowBox);
        buttonBox.setSpacing(5);
        buttonBox.prefWidthProperty().bind(mainBox.widthProperty());

        return buttonBox;
    }

    private VBox mainVBox() {

        mainBox = new VBox();

        mainBox.prefWidthProperty().bind(mainStage.widthProperty().multiply(0.8));

        mainBox.setPadding(new Insets(10, 10, 10 ,10));
        mainBox.getChildren().add(mainGrid());

        return mainBox;
    }

    private void initButton() {

        open = new Button("Open");
        open.setPrefWidth(100);
        save = new Button("Save");
        save.setPrefWidth(100);
        neu = new Button("New");
        neu.setPrefWidth(100);
        settings = new Button("Settings");
        settings.setPrefWidth(100);
        table = new Button("Table");
        table.setPrefWidth(100);
        row = new Button("Row");
        row.setPrefWidth(100);
        column = new Button("Column");
        column.setPrefWidth(100);
        date = new Button("Date");
        date.setPrefWidth(100);
    }


    private TableView initTable() {
        tableView = new TableView<>();

        // Create column UserName (Data type of String).
        TableColumn<String, String> userNameCol //
            = new TableColumn<>("User Name");

        // Create column Email (Data type of String).
        TableColumn<String, String> emailCol//
            = new TableColumn<>("Email");

        // Create 2 sub column for FullName.
        TableColumn<String, String> firstNameCol //
            = new TableColumn<>("First Name");

        TableColumn<String, String> lastNameCol //
            = new TableColumn<>("Last Name");


        // Active Column
        TableColumn<String, Boolean> activeCol//
            = new TableColumn<>("Active");

        tableView.getColumns().addAll(userNameCol, emailCol, firstNameCol, lastNameCol, activeCol);

        for (int i = 0; tableView.getColumns().size() < i; i++){

            tableView.getColumns().get(i).setMinWidth(100);
        }

        tableView.setColumnResizePolicy(TableView.CONSTRAINED_RESIZE_POLICY);

        return tableView;
    }
}

最后,我希望在加载文件后,列的宽度仍应为TableView宽度,该宽度由列数所指定,期望宽度将小于100px。在这种情况下,每个宽度应为100px,并出现一个滚动窗格(忽略此处的滚动条)。

谢谢您的帮助!

1 个答案:

答案 0 :(得分:2)

永远不会设置您的最小宽度限制...

只需替换一下:

 for (int i = 0; tableView.getColumns().size() < i; i++){

对此:

 for (int i = 0; i < tableView.getColumns().size(); i++) {

或者甚至更好地使用forEach

 tableView.getColumns().forEach(column -> column.setMinWidth(100));