JavaFX: Set every column minimum width

poisn :

I'm trying to set every column minimum width to 100px. I prefer to use tableView.setColumnResizePolicy(TableView.CONSTRAINED_RESIZE_POLICY);

Explanation

If you set TableView.setColumnResizePolicy(TableView.CONSTRAINED_RESIZE_POLICY) all columns will be equally resized until the TableViews maximum width is reached.

Explanation from

At the moment the width of each column is determined by the width of the TableViewdevided by the number of columns.

I tried to set every column the minimum width but that didn't work. I've also saw that people just created their own callback for the setColumnResizePolicy but I couldn't implement my idea of what should happen.

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

In the end I expect that after I load the file, the width of the column should still be the TableView width devided by the number of columns expect the width would be < 100px. In this case every width should be 100px and a scrollpane appears (ignores the scrollbar here).

Thank you for your help!

vl4d1m1r4 :

Your constraint of min width is never set...

Just replace this:

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

to this:

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

Or even better use forEach:

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

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=139898&siteId=1