具有条件颜色变化的 JavaFx TableView ProgressBar

标签 java javafx tableview progress-bar

需要: 能够根据多线程程序中的条件更改 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/

相关文章:

java - 带有 OpenJDK 10 和 OpenJFX 的 MacOS X 上的 JavaFX 中的 HeadlessException

ios - 从 TableView 到标签的数组数据

java - 如何解决 H2 "Error executing DDL"错误?

java - 在哪里放置检查图中两条边是否平行的方法?

macos - 构建 .dmg 包需要 Apple 开发者 ID

java - 在 javafx 中获取应用程序实例

java - edittext 中的值在获得焦点之前不会显示。安卓

java - Hibernate - 如何创建 pojos 列表?

swift - 如何计算 UITableView 单元格中的总值?

ios - Swift:如何通过单击按钮 UITableViewCell 创建 UIActionSheet