I already took help of this previous answer but I still have a problem
I want to change the colour of progressbar as slider's value changes , but my progressbar stays always in RED colour and doesn't change if it goes beyond 2,4,6. What am I missing here ? (value : min is 0 and max is 10)
slider
is id of slider and pbar
is id of progressbar.
piece of code :
private static final String RED_BAR = "red-bar";
private static final String YELLOW_BAR = "yellow-bar";
private static final String ORANGE_BAR = "orange-bar";
private static final String GREEN_BAR = "green-bar";
private static final String[] barColorStyleClasses = { RED_BAR, ORANGE_BAR, YELLOW_BAR, GREEN_BAR };
public void initialize(URL location, ResourceBundle resources) {
slider.valueProperty().addListener(new ChangeListener<Number>() {
public void changed(ObservableValue<? extends Number> ov, Number old_val, Number new_val) {
pbar.setProgress(new_val.doubleValue()/10);
}
});
pbar.progressProperty().addListener(new ChangeListener<Number>() {
@Override public void changed(ObservableValue<? extends Number> observable, Number oldValue, Number newValue) {
double progress = newValue == null ? 0 : newValue.doubleValue();
if (progress < 2) {
setBarStyleClass(pbar, RED_BAR);
} else if (progress < 4) {
setBarStyleClass(pbar, ORANGE_BAR);
} else if (progress < 6) {
setBarStyleClass(pbar, YELLOW_BAR);
} else {
setBarStyleClass(pbar, GREEN_BAR);
}
}
private void setBarStyleClass(ProgressBar bar, String barStyleClass) {
bar.getStyleClass().removeAll(barColorStyleClasses);
bar.getStyleClass().add(barStyleClass);
}
});
}
CSS file :
.root { -fx-background-color: cornsilk; -fx-padding: 15; }
.pbar { -fx-box-border: goldenrod; }
.green-bar { -fx-accent: green; }
.yellow-bar { -fx-accent: yellow; }
.orange-bar { -fx-accent: orange; }
.red-bar { -fx-accent: red; }