What I actually want to do however, is change the color of the actual bar (the value).
I can see here that there are two sub-parts of the progressbar (https://vaadin.com/components/vaadin-progress-bar/html-api/elements/Vaadin.ProgressBarElement). How do I access the “value” part of the progressbar in Java? I assume this is the actual part I want since it says “Progress-bar’s foreground”.
I don’t think it’s possible using inline styles like you do. Instead, add a CSS file specifically for vaadin-progress-bar
Over your mainview class (if you want your stylings to apply globally) or over a more specific view or component that contains your ProgressBar, add the annotation CssImport using the themeFor attribute to specify the progressbars html tag.
@CssImport(value = "./frontend/styles/myProgressBarStyles.css", themeFor = "vaadin-progress-bar")
public class MainView extends VerticalLayout {
...
}
<!-- frontend/styles/myProgressBarStyles.css -->
<!-- if unclear where the frontend folder should be located in the project => https://stackoverflow.com/a/57553974/3441504 -->
[part="value"]
{
background-color: red;
}
I don’t think it’s possible using inline styles like you do. Instead, add a CSS file specifically for vaadin-progress-bar
Over your mainview class (if you want your stylings to apply globally) or over a more specific view or component, add the annotation CssImport using the themeFor attribute to specify the progressbars html tag.
@CssImport(value = "./frontend/styles/myProgressBarStyles.css", themeFor = "vaadin-progress-bar")
public class MainView extends VerticalLayout {
...
}
Thank you. This works but I want to change the color dynamically during runtime. I have a score attribute ranging from 0 - 1.0 and depending on the current value, I want to change the color from red → orange → green.
I can see that the ProgressBar class has a setClassName method, can I declare classes within the css-file and set them using this method?
I can see that the ProgressBar class has a setClassName method, can I declare classes within the css-file and set them using this method?
Yes that’s how I would approach this scenario. It should work as long as you have a predefined set of colors/classes so you can prepare all the CSS class styles accordingly.
and then whenever you call progressBar.setValue you determine which color you want based on the value and set the correct class name with progressBar.setClassName("progress-green"); for example.
I can see that the ProgressBar class has a setClassName method, can I declare classes within the css-file and set them using this method?
Yes that’s how I would approach this scenario. It should work as long as you have a predefined set of colors/classes so you can prepare all the CSS class styles accordingly.
and then whenever you call `progressBar.setValue` you determine which color you want based on the value and set the correct class name with `progressBar.setClassName("progress-green");` for example.