I've created a TableView which contains a checkbox column(isSelected), and three info column(name, surname, job). I want to disable some checkbox according to user info. For example, if user name is "Peter", the checkbox near the Peter will be disabled. But I couldn't. Here is some of 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);
}
}
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());
}
}
};
});
Thank you @Ammar, I've resolved it like this make use of your 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());
}
}
};
});
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.