复选框上的Javafx tableview取消选中,禁用同一行但不同列上的按钮
Javafx tableview on checkbox uncheck, disable button on same row but different column
下图解释了我如何填充 table:
如您所见,一旦取消选中相关行的 Collected
列复选框,我需要禁用 Installments
列按钮,反之亦然。
到目前为止,这是我的方法:
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);
}
但是这种方式不符合要求,按钮一直处于启用状态。任何帮助将不胜感激。谢谢你。
不要在您的项目中放置节点 class。这样你就破坏了 TableView
的主要好处:将节点数量限制为显示内容所需的节点数量。
您最好在 Member
对象中使用 BooleanProperty
,使用 CheckBoxTableCell
显示 CheckBox
es 并为分期付款列使用自定义单元格:
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;
}
...
}
下图解释了我如何填充 table:
如您所见,一旦取消选中相关行的 Collected
列复选框,我需要禁用 Installments
列按钮,反之亦然。
到目前为止,这是我的方法:
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);
}
但是这种方式不符合要求,按钮一直处于启用状态。任何帮助将不胜感激。谢谢你。
不要在您的项目中放置节点 class。这样你就破坏了 TableView
的主要好处:将节点数量限制为显示内容所需的节点数量。
您最好在 Member
对象中使用 BooleanProperty
,使用 CheckBoxTableCell
显示 CheckBox
es 并为分期付款列使用自定义单元格:
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;
}
...
}