JavaFx TableView 进度条,具有条件颜色更改



需要:能够根据多线程程序中的条件更改 TableView 中进度条的颜色

研究:我指的是链接 - Link1,基于它,我可以在由多个线程更新的 TableView 中设置进度条。根据链接 - Link2 我需要能够更改颜色。此外,谷歌搜索也是如此,但无法让它工作并因此发布。

问题:我已经完成了绑定并更新了属性,但它似乎不起作用。进度条正在根据值进行更新,这很好。

无法弄清楚如何根据条件更改颜色。希望面临的问题是明确的,投入/指导将不胜感激。

代码(控制器类的相关部分(:

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>并通过覆盖 ProgressBarTableCell 的updateItem(Double item, boolean empty)方法合并所需的配色方案

按照 iCoder 的建议,我也最终复制/粘贴ProgressBarTableCell:-(

import javafx.beans.value.ObservableValue;
import javafx.scene.control.ProgressBar;
import javafx.scene.control.TableCell;
import javafx.scene.control.TableColumn;
import javafx.scene.paint.Color;
import javafx.util.Callback;
import javafx.util.Pair;
public class ProgressBarTableCellEx<S> extends TableCell<S, Pair<Color, Double>> {
public static <S> Callback<TableColumn<S,Pair<Color, Double>>, TableCell<S,Pair<Color, Double>>> forTableColumn() {
return param -> new ProgressBarTableCellEx<S>();
}
private final ProgressBar progressBar;
private ObservableValue<Pair<Color, Double>> observable;
public ProgressBarTableCellEx() {
this.getStyleClass().add("progress-bar-table-cell");
this.progressBar = new ProgressBar();
this.progressBar.setMaxWidth(Double.MAX_VALUE);
}
/** {@inheritDoc} */
@Override public void updateItem(Pair<Color, Double> item, boolean empty) {
super.updateItem(item, empty);
if (empty) {
setGraphic(null);
} else {
progressBar.progressProperty().unbind();
progressBar.styleProperty().unbind();
final TableColumn<S,Pair<Color, Double>> column = getTableColumn();
observable = column == null ? null : column.getCellObservableValue(getIndex());
if (observable != null) {
progressBar.progressProperty().bind(observable.map(Pair::getValue));
progressBar.styleProperty().bind(observable.map(Pair::getKey).map(color -> "-fx-accent:"+toString(color)));
} else if (item != null) {
progressBar.setProgress(item.getValue());
progressBar.setStyle("-fx-accent:"+toString(item.getKey()));
}
setGraphic(progressBar);
}
}
private String toString(Color color) {
return color.toString().replace("0x", "#");
}
}

最新更新