[英]Javafx tableview on checkbox uncheck, disable button on same row but different column
Below image explains how I have populated my table: 下图说明了如何填充表格:
As you can see, I need to disable the Installments
column button once the Collected
column checkbox of related row is unchecked and vise versa. 如您所见,一旦未选中相关行的
Collected
列”复选框,我需要禁用“ Installments
列按钮,反之亦然。
Here is my approach so far : 到目前为止,这是我的方法:
colected_column.setCellValueFactory((TableColumn.CellDataFeatures<Member, CheckBox> param) -> {
Member mRow = param.getValue(); // type objects contained within the TableView
CheckBox checkBox = new CheckBox();
checkBox.selectedProperty().addListener((ov, old_val, new_val) -> {
// javafx.scene.control.Button typed ObservableValue returns as cell value
Button button = installments_column.getCellData(mRow);
button.setDisable(!new_val);
});
...
return new SimpleObjectProperty<>(checkBox);
}
But this approach does not meet the requirement, button stays enable all the time. 但是这种方法不符合要求,按钮一直保持启用状态。 Any help would be appreciable.
任何帮助将是可观的。 Thank you.
谢谢。
Don't put nodes in your item class. 不要在项目类中放置节点。 This way you ruin the main benefit of
TableView
: limiting the amount of nodes to the one it needs to display the content. 这样,您将破坏
TableView
的主要优点:将节点数限制为显示内容所需的节点数。
You should better use a BooleanProperty
in your Member
object, use CheckBoxTableCell
to display the CheckBox
es and use custom cells for the installments column: 您最好在
Member
对象中使用BooleanProperty
,使用CheckBoxTableCell
显示CheckBox
并将自定义单元格用于分期付款列:
TableColumn<Member, Boolean> colected_column = ...;
colected_column.setCellValueFactory((TableColumn.CellDataFeatures<Member, Boolean> param) -> {
Member mRow = param.getValue(); // type objects contained within the TableView
return nRow.collectedProperty();
});
colected_column.setCellFactory(CheckBoxTableCell.forTableColumn(colected_column));
TableColumn<Member, Boolean> installmentsColumn = ...;
installmentsColumn.setCellValueFactory(cd -> cd.getValue().collectedProperty());
installmentsColumn.setCellFactory(column -> new TableCell<Member, Boolean>() {
private final Button button = new Button("View Info");
{
button.setOnAction(evt -> {
Member member = (Member) getTableRow().getItem();
// TODO: handle button click
});
}
@Override
protected void updateItem(Boolean item, boolean empty) {
super.updateItem(item, empty);
if (empty || item == null) {
setGraphic(null);
} else {
setGraphic(button);
button.setDisable(!item);
}
}
});
public class Member {
private final BooleanProperty collected = new SimpleBooleanProperty(true);
public void setCollected(boolean value) {
collected.set(value);
}
public boolean isCollected() {
return collected.get();
}
public BooleanProperty collectedProperty() {
return collected;
}
...
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.