更新表查看行外观编辑的信息
我有一些措施来更改某些 TableView 行的外观。该行应以笔触和红色显示文本。实际上,我可以用红色显示它,但仍然无法进行笔触。这是我用来更改行外观的 css 类:
.itemCancelado {
-fx-strikethrough: true;
-fx-text-fill: red;
}
当用户将项目标记为已取消时,将添加此样式类:
public class ItemCanceladoCellFactory implements Callback<TableColumn, TableCell> {
@Override
public TableCell call(TableColumn tableColumn) {
return new TableCell<ItemBean, Object>() {
@Override
public void updateItem(Object item, boolean empty) {
super.updateItem(item, empty);
setText(empty ? "" : getItem().toString());
setGraphic(null);
int indice=getIndex();
ItemBean bean=null;
if(indice<getTableView().getItems().size())
bean = getTableView().getItems().get(indice);
if (bean != null && bean.isCancelado())
getStyleClass().add("itemCancelado");
}
};
}
}
这里还有另一个问题,标记为已取消的行仅在用户在可观察列表中添加或删除元素时更改颜色。有没有办法强制更新表视图?
编辑的信息
我将 ItemBean 类更改为使用布尔属性,它解决了部分问题:
public class ItemBean {
...
private BooleanProperty cancelado = new SimpleBooleanProperty(false);
...
public Boolean getCancelado() {
return cancelado.get();
}
public void setCancelado(Boolean cancelado){
this.cancelado.set(cancelado);
}
public BooleanProperty canceladoProperty(){
return cancelado;
}
}
不幸的是,只有列“cancelado”(当它最终起作用时将被隐藏或删除)改变了外观:
在这里,我配置列和表:
public class ControladorPainelPreVenda extends ControladorPainel {
@FXML
private TableView<ItemBean> tabelaItens;
private ObservableList<ItemBean> itens = FXCollections.observableArrayList();
...
private void configurarTabela() {
colunaCodigo.setCellValueFactory(new MultiPropertyValueFactory<ItemBean, String>("produto.id"));
colunaCodigo.setCellFactory(new ItemCanceladoCellFactory());
colunaDescricao.setCellValueFactory(new MultiPropertyValueFactory<ItemBean, String>("produto.descricao"));
colunaDescricao.setCellFactory(new ItemCanceladoCellFactory());
colunaLinha.setCellValueFactory(new MultiPropertyValueFactory<ItemBean, String>("produto.nomeLinha"));
colunaLinha.setCellFactory(new ItemCanceladoCellFactory());
colunaQuantidade.setCellValueFactory(new PropertyValueFactory<ItemBean, BigDecimal>("quantidade"));
colunaQuantidade.setCellFactory(new ItemCanceladoCellFactory());
colunaValorLiquido.setCellValueFactory(new PropertyValueFactory<ItemBean, BigDecimal>("valorLiquido"));
colunaValorLiquido.setCellFactory(new ItemCanceladoCellFactory());
colunaValorTotal.setCellValueFactory(new PropertyValueFactory<ItemBean, BigDecimal>("valorTotal"));
colunaValorTotal.setCellFactory(new ItemCanceladoCellFactory());
colunaCancelado.setCellValueFactory(new PropertyValueFactory<ItemBean, Boolean>("cancelado"));
colunaCancelado.setCellFactory(new ItemCanceladoCellFactory());
tabelaItens.setItems(itens);
}
...
}
如何更新所有列?