【发布时间】:2019-01-17 19:20:58
【问题描述】:
需要: 能够在多线程程序中根据条件改变TableView中ProgressBar的颜色
研究: 我参考了链接 - Link1,据此我可以在 TableView 中设置由多个线程更新的 ProgressBar。根据链接 - Link2 我需要能够更改颜色。此外,谷歌搜索也是如此,但无法使其正常工作并因此发布。
问题: 我已经完成了绑定和更新属性,但它似乎不起作用。进度条会根据值更新,这很好。
无法弄清楚如何根据条件更改颜色。希望所面临的问题很清楚,非常感谢您的意见/指导。
代码(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");
}
}
【问题讨论】:
标签: java javafx tableview progress-bar