How to change row height of TableView on the fly?


Question

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.

1
1
2/21/2014 11:06:09 PM

Accepted Answer

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);
5
2/24/2014 8:10:51 AM

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:)

enter image description here


Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon