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.