1

My Problem
I'm trying to resize a column and, it works as inteended at first. But as soon as the width of the column is the same as the minimum width, it doesn't work anymore.

It's like stuck. As user, you can't make it bigger unless you resize the whole window.

I know roughly what the problem is. I think it is because there is no space to make a cell bigger.

What I tried
What I know is that if I removed this line, it would work.

tableView.setColumnResizePolicy(TableView.CONSTRAINED_RESIZE_POLICY);

But I need this line for my programm, because the number of columns is different from file to file. I also need this line because every column should be the same size after loading the file (number of columns / width from tableview = column width).

MCVE

public class GuiLayout extends Application {

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

    private TableView<String> tableView;

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

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


        initTable();

        mainStage = new Stage();
        mainScene = new Scene(mainVBox(), 1000, 600);

        ObservableList observableList = FXCollections.observableArrayList("1", "1234", "Avenue 1", "17", "Second", "Married", "M", "admin", "admin@admin.com", "Just", "Test", "no");

        tableView.setItems(observableList);

        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(tableView, 0, 2);

        return gridPane;
    }


    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 TableView initTable() {
        tableView = new TableView<>();

        TableColumn<String, String> numberCol
            = new TableColumn<>("Nr.");

        TableColumn<String, String> plzCol
            = new TableColumn<>("PLZ");

        TableColumn<String, String> adressCol
            = new TableColumn<>("Adress");

        TableColumn<String, String> houseNumber
            = new TableColumn<>("House number");

        TableColumn<String, String> floorCol
            = new TableColumn<>("Floor");

        TableColumn<String, String> familyStatusCol
            = new TableColumn<>("Family Status");

        TableColumn<String, String> genderCol
            = new TableColumn<>("Gender");

        TableColumn<String, String> userNameCol //
            = new TableColumn<>("User Name");

        TableColumn<String, String> emailCol//
            = new TableColumn<>("Email");

        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(numberCol, plzCol, adressCol, houseNumber, floorCol, familyStatusCol, genderCol, 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 you can resize the column (make it bigger) when every columns has the minimum width.

Thank you for your help.

Edit:

I know that I need a customized Callback but I don't really know how I should realized it.

I tried things like setting the Property to tableView.setColumnResizePolicy(TableView.UNCONSTRAINED_RESIZE_POLICY); and set the size of the column hardcoded.

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

            tableView.getColumns().get(o).prefWidthProperty().bind(tableView.widthProperty().divide(tableView.getColumns().size()));

Can anyone help me with the Callback? I'm not asking you if you can make the Callback for me (but if you want go for it) more I'm asking if you can give me a hint or something how I could realise it.

poisn
  • 437
  • 2
  • 17
  • 3
    there's a conflict between your resizePolicy and the minwidth: > 10 columns with min 100 each already fills the scene, so what should the policy do? one way or other it will violate one of the constraints. Slightly unrelated: having many width bindings doesn't necessarily improve the layout - instead use the layout that fits your needs and let that do its job. – kleopatra Aug 27 '19 at 08:06
  • @kleopatra Okay thank you. But is there a way to "handle" this problem with a scrollpane? Because as I said there is no space to make a cell bigger, but can't I make a column bigger and there will appear a scrollpane without an impact on the scene? – poisn Aug 27 '19 at 08:13
  • if you want to keep column width the same no matter how wide the window is, the most clean solution is a custom resizePolicy. Or try width bindings between the columns (that is one property and all columns are bound to that) - would expect problems, though, resizing (implemented in Nested/TableColumn) is not bug-free – kleopatra Aug 27 '19 at 08:21
  • Can you clarify what you are looking for now? What exactly you want to achieve and what do you expect from answer? As you already know why you can't resize it due to first comment. – GotoFinal Sep 02 '19 at 14:19
  • @GotoFinal a way to solve my problem with a customized callback – poisn Sep 03 '19 at 06:07
  • I've run your code with `TableView.UNCONSTRAINED_RESIZE_POLICY` and couldn't produce any behaviour which is not the way you want it to be as I understand you. Please explain in detail which behaviour bothers you if resize policy is set to `TableView.UNCONSTRAINED_RESIZE_POLICY`. – Lesurglesum Sep 08 '19 at 21:20
  • May be this [post](https://stackoverflow.com/questions/14095708/fxml-set-tableview-column-resize-policy) could help you – Meziane Sep 09 '19 at 10:04
  • @Lesurglesum This program is just a mcve. My main program loads files and shows the data in the table. The problem is, each file has a different amount of columns. After loading the file, the width of the column is determined by the width from the table devided by the amount of columns. If its to many columns (so that a column is under 100px) each column will be on the minimum width 100px. But if every column is on the minimum width, I can't make a column bigger (except I resize the whole window). – poisn Sep 10 '19 at 06:31

0 Answers0