Search code examples
javajavafxtableviewprogress-bar

JavaFx TableView ProgressBar with Conditional Color change


Need: To be able to change the color of the ProgressBar in TableView depending on the condition in a multi-threaded program

Research: I refer to link - Link1 based on which I am able to setup the ProgressBar in TableView being updated by multiple threads. As per link - Link2 I need to be able to change the color. In addition did google search as well, but unable to get it working & hence posting.

Issue: I have done a bind & updating the property, but it does not seem to work. The progress bar is getting updated as per value, which is fine.

Unable to figure out how to get the color to change based on condition. Hope the issue faced is clear, Inputs / guidance will be much appreciated.

Code(relevant portions from Controller class):

    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");
            }
        }

Solution

  • Don't know why my question has been down-voted, despite following the rules.

    Anyway after further trials managed to get it working & if anyone looking for something similar, all you need to do is to extend - TableCell<S, Double> & incorporate the color scheme required by overriding the updateItem(Double item, boolean empty) method of ProgressBarTableCell