具有条件颜色更改的JavaFx TableView ProgressBar

时间:2018-08-10 08:19:22

标签: java javafx tableview progress-bar

需要: 为了能够根据多线程程序中的条件在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");
            }
        }

1 个答案:

答案 0 :(得分:0)

即使遵循规则,也不知道为什么我的问题被否决了。

无论如何,经过进一步的试验设法使其正常工作,并且如果有人在寻找相似的东西,您所需要做的就是扩展-TableCell<S, Double>并通过覆盖updateItem(Double item, boolean empty)方法来合并所需的配色方案。 ProgressBarTableCell

相关问题