Reputation: 1894
I'd like to change the row height of JavaFX TableView when user selects/deselects a CheckBox. The only way I could find is via CSS, so I tried:
if (someCheckBox.isSelected())
tableView.setStyle(".table-row-cell {-fx-cell-size: 60px;}");
else
tableView.setStyle(".table-row-cell {-fx-cell-size: 20px;}");
But this does not work. Any suggestion would be appreciated.
Upvotes: 1
Views: 13029
Reputation: 772
Here is my solution of this problem (it's only first "Workaround", but "hight works fine):
column8.setCellFactory(column -> {
return new TableCell<Anfrage, Set<Email>>() /*Or whathever you have*/ {
@Override
protected void updateItem(Set<Email> item, boolean empty) {
super.updateItem(item, empty);
if (item == null || empty) {
setGraphic(null);
setText("");
} else {
VBox graphic = new VBox();
if (item.size()>1) {
List<Email> sortedMailsList = new ArrayList<>();
sortedMailsList.addAll(item);
Collections.sort(sortedMailsList, new Comparator<Email>(){
@Override
public int compare(Email o1, Email o2) {
if (o1.getEmailVersandt() != null) {
if (o2.getEmailVersandt() != null) {
return o1.getEmailVersandt().compareTo(o2.getEmailVersandt());
} else {
return 1;
}
} else {
if (o2.getEmailVersandt() != null) {
return -1;
} else {
return 0;
}
}
}
});
sortedMailsList.stream().forEach((emailObj) -> {
graphic.getChildren().add((new Label(emailObj.toString())));
});
} else {
for (Email emailObj : item) {
graphic.getChildren().add((new Label(emailObj.toString())));
}
}
this.setMaxHeight(20.0*item.size());
setGraphic(graphic);
}
}
};
});
For me it's also sort "Mails" (Date field which is nullable), but i think you don't need it. Main points are: 1. Set cell factory 2. Set height in "updateItem". And that's it:)
Upvotes: 2
Reputation: 1894
I found out that the JavaFX 8 introduces setFixedCellSize property, which meets my requirements as I need all rows to have the same height. So this is the solution:
if (someCheckBox.isSelected())
tableView.setFixedCellSize(60.0);
else
tableView.setFixedCellSize(20.0);
Upvotes: 10