需要: 能够根据多线程程序中的条件更改 TableView 中 ProgressBar 的颜色
研究: 我引用链接 - Link1基于此,我可以在 TableView 中设置由多个线程更新的 ProgressBar。根据链接 - Link2我需要能够改变颜色。此外,谷歌也进行了搜索,但无法使其正常工作,因此发布。
问题: 我已经完成了绑定(bind)并更新属性,但它似乎不起作用。进度条根据值进行更新,这很好。
无法弄清楚如何根据条件改变颜色。希望所面临的问题很清楚,非常感谢您的意见/指导。
代码( Controller 类的相关部分):
StringProperty colorProperty = new SimpleStringProperty();
.
.
.
progressBar.setCellValueFactory(new PropertyValueFactory<>("pBar));
progressBar.setCellFactory(ProgressBarTableCell.forTableColumn());
// Bind property for progress Bar Color Change
progressBar.styleProperty().bind(colorProperty);
.
.
.
private static void updateTableView(List<TableViewCust> list, ModelDataFeed apiDataFeed) {
list.forEach(e -> {
double pBarValue = ((apiDataFeed.getMax() - apiDataFeed.getMin()) / e.getAvg());
e.setProgressBar(pBarValue / 5);
setProgressBarColor(pBarValue / 5));
});
}
.
.
.
// Set Colour to Progress Bar
private static void setProgressBarColor(double pBarValue) {
if (pBarValue <= 0.2) {
colorProperty.setValue("-fx-accent: lightskyblue");
} else if (pBarValue <= 0.4) {
colorProperty.setValue("-fx-accent: palegreen");
} else if (pBarValue <= 0.6) {
colorProperty.setValue("-fx-accent: orange");
} else if (pBarValue <= 0.8) {
colorProperty.setValue("-fx-accent: orchid");
} else {
colorProperty.setValue("-fx-accent: red");
}
}
最佳答案
不知道为什么我的问题被否决,尽管遵守了规则。
无论如何,经过进一步的试验设法让它工作,如果有人寻找类似的东西,你需要做的就是扩展 - TableCell<S, Double>
并合并覆盖 updateItem(Double item, boolean empty)
所需的配色方案ProgressBarTableCell方法
关于具有条件颜色变化的 JavaFx TableView ProgressBar,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51782049/