Search code examples
javafxjava-8javafx-8javafx-tableviewjavafx-bindings

JavaFX Disable TableColumn based on checkbox state


Am looking to disable a TableColumn<CustomObject, String> tableColumn based on a field value in the CustomObject only when the TableColumn<CustomObject, Boolean> tableColumnTwo checkbox is checked. I can disable the textbox inside public void updateItem(String s, boolean empty) however not sure how to check the state of checkbox inside updateItem Below is the relevant code snippet, would highly appreciate if anyone can shed light on this

@FXML 
private TableColumn<CustomObject, Boolean> tableColumnTwo;
@FXML 
private TableColumn<CustomObject, String> tableColumn;

tableColumn.setCellFactory(
                     new Callback<TableColumn<CustomObject, String>, TableCell<CustomObject, String>>() {

                         @Override
                         public TableCell<CustomObject, String> call(TableColumn<CustomObject, String> paramTableColumn) {
                             return new TextFieldTableCell<CustomObject, String>(new DefaultStringConverter()) {
                                 @Override
                                 public void updateItem(String s, boolean empty) {
                                     super.updateItem(s, empty);
                                     TableRow<CustomObject> currentRow = getTableRow();
                                     if(currentRow.getItem() != null && !empty) {
                                         if (currentRow.getItem().getPetrified() == false) { // Need to check if checkbox is checked or not
                                             setDisable(true);
                                             setEditable(false);
                                             this.setStyle("-fx-background-color: red");
                                         } else {
                                             setDisable(false);
                                             setEditable(true);
                                                                                             setStyle("");
                                         }
                                     }
                                 }
                             };
                         }

                     });

Solution

  • You can add a listener on the checkbox, which when checked will cause the table refresh.

    data = FXCollections.observableArrayList(new Callback<CustomObject, Observable[]>() {
    
                @Override
                public Observable[] call(CustomObject param) {
                    return new Observable[]{param.petrifiedProperty()};
                }
        });
    
    
    data.addListener(new ListChangeListener<CustomObject>() {
    
            @Override
            public void onChanged(ListChangeListener.Change<? extends CustomObject> c) {
                while (c.next()) {
                    if (c.wasUpdated()) {
                        tableView.setItems(null); 
                        tableView.layout(); 
                        tableView.setItems(FXCollections.observableList(data)); 
                    }
                }
            }
        });
    

    Your cellFactory would remain the same and would get called when a checkbox is checked/unchecked.