0

I have the follow code:

.progress-bar > .bar {
    -fx-background-color: linear-gradient(
    from 0em 0.75em to 0.75em 0px,
    repeat,
    -fx-accent 0%,
    -fx-accent 49%,
    derive(-fx-accent, 30%) 50%,
    derive(-fx-accent, 30%) 99%
);}

Is it possible to set it by code? If yes, how to do that?

I want to set by code because I want to solve another problem, click here

One guy told me to use lookup get selector, but it return null.

the following code is what I did? Could you tell me what's wrong?

Node bar = progress.lookup(".bar"); 

any help would be appreciated.

Community
  • 1
  • 1
Michael
  • 425
  • 1
  • 4
  • 14

1 Answers1

1

You can use the lookup to get the node and then call setStyle().

lookups are not very robust, and generally will not work (i.e. will return null) until css has been applied to the scene graph. This typically happens on the first rendering pass. So, as a minimum, you need to do this after the Stage has been shown. You may find you need to wrap it in a Platform.runLater(...) to make it work.

If you are only wanting to change the background color, and need to do so dynamically, consider using a looked-up color instead.

Something like:

css file:

.progress-bar {
  my-bar-color: linear-gradient(
    from 0em 0.75em to 0.75em 0px,
    repeat,
    -fx-accent 0%,
    -fx-accent 49%,
    derive(-fx-accent, 30%) 50%,
    derive(-fx-accent, 30%) 99%
);}
.progress-bar > .bar {
  -fx-background-color: my-bar-color ;
}

And now in Java you can do

String color = ... ;
progress.setStyle("my-bar-color: "+color);

I don't know if this will work for your animation, but it's worth a try...

James_D
  • 201,275
  • 16
  • 291
  • 322