JavaFX TableView Disable CheckBox

I created a TableView which contains a checkbox column (isSelected) and three columns of information (first name, last name, task). I want to disable some checkboxes according to user info. For example, if the username is "Peter", the checkbox next to Peter will be disabled. But I couldn't. Here is my code:

Person.java

public class Person {

private final SimpleStringProperty name;
private final SimpleStringProperty surname;
private final SimpleStringProperty job;
private final BooleanProperty isSelected;

public Person(SimpleStringProperty name, SimpleStringProperty surname, SimpleStringProperty job,
        BooleanProperty isSelected) {
    super();
    this.name = name;
    this.surname = surname;
    this.job = job;
    this.isSelected = isSelected;
}

public SimpleStringProperty getName() {
    return name;
}

public SimpleStringProperty getSurname() {
    return surname;
}

public SimpleStringProperty getJob() {
    return job;
}

public BooleanProperty getIsSelected() {
    return isSelected;
}



}

      

Controller.java

public class Controller {

@FXML
private final TableView<Person> fxPersonTableView;

@FXML
private final TableColumn<Person, Boolean> fxSelectColumnCheckbox;

@FXML
private final TableColumn<Person, String> fxNameTableColumn;

@FXML
private final TableColumn<Person, String> fxSurnameTableColumn;

@FXML
private final TableColumn<Person, String> fxJobTableColumn;

private ObservableList<Person> persons;

private void createTableView(){

    fxSelectColumnCheckbox.setCellValueFactory(c -> c.getValue().getIsSelected());

    fxSelectColumnCheckbox.setCellFactory(CheckBoxTableCell.forTableColumn(c -> {
    //I guess here I will put some control like as name check and disable 
    //the checkbox in this row. But I can't reach the checkbox individually.
        return persons.get(c).getIsSelected();
    }));

    fxNameTableColumn.setCellValueFactory(c -> persons.get(c).getName());
    fxSurnameTableColumn.setCellValueFactory(c -> persons.get(c).getSurname());
    fxJobTableColumn.setCellValueFactory(c -> persons.get(c).getJob());
    fxPersonTableView.getSelectionModel.setSelectionMode(SelectionMode.MULTIPLE);
    fxPersonTableView.setItems(persons);


}



}

      

+4


source to share


2 answers


Thanks @Ammar, I resolved it using this answer:



fxSelectColumnCheckbox.setCellFactory(column -> {
        return new CheckBoxTableCell<Person, Boolean>() {
            @Override
            public void updateItem(Boolean item, boolean empty) {
                super.updateItem(item, empty);

                TableRow<Person> currentRow = getTableRow();
                this.setDisable(false); // it is required to fit default state
                if (currentRow.getItem() != null && !empty) {
                    if (currentRow.getItem().getName().equals("Peter")) {
                        this.setDisable(true);
                        setStyle(fxPersonTableView.getStyle());
                    }
                } else {

                    setStyle(fxPersonTableView.getStyle());
                }
            }
        };
    });

      

+1


source


This will work for you.



fxNameTableColumn.setCellFactory(column -> {
                return new TableCell<Person, String>() {
                    @Override
                    protected void updateItem(String item, boolean empty) {
                        super.updateItem(item, empty);
                        // Iterating through Table rows
                        TableRow<Person> currentRow = getTableRow();
                        if (item != null && !empty) {
                            if (item.equals("Peter")) {
                                // a row containing Peter name is found
                                // disable your check box  
                                setStyle(fxPersonTableView.getStyle());
                            } 
                        } else {
                            setStyle(fxPersonTableView.getStyle());
                        }
                    }
                };
            });

      

+1


source







All Articles