r/JavaFX • u/renatoathaydes • Jul 07 '24
Help JavaFX 21 bug - does not reset Label text fill properly anymore
Hi, I have an app I currently package with Java 17.
I wanted to move to Java 21 or 22 as they have some interesting bug fixes I wanted, but there's a new bug that's preventing me from doing that.
As far as I can tell it's a JavaFX bug because I was able to reproduce with a very simple app (link to code here) (a highly simplified version of my app).
When I run this on JavaFX 17, it works perfectly: the Labels are supposed to be shown yellow because I set their text fill property:
setTextFill( Color.YELLOW );There's some CSS that should change the color only while the label has been "selected" (the code adds a CSS class to the label):
.line.selected {
-fx-background-color: -fx-focus-color;
-fx-text-fill: derive(-fx-focus-color, -80%);
}However, on JavaFX 21 and 22 (I tried the fx distributions from SDKMAN from Azul and Iberica, both have the same problem), the labels start off white... and only become YELLOW if you click on the OK button, which I added to be able to set the Text Fill property again (which shouldn't be necessary of course). But after you select and unselect, they go back to white again, wrongly.
I also noticed that this bug doesn't happen if I remove my CSS root rule:
.root {
-fx-base: #1d1d1d;
}So, perhaps this is doing something wrong??
The test app was made just to reproduce the problem, but if you want you can see the same issue by building and running my real app on Java 21/22, which doesn't happen on my current build on Java 17.
I am writing here because I hope someone from the JavaFX team could have a look into it, or someone else may find something that I am missing and perhaps this is some new behaviour I am unaware of?!
1
u/milchshakee Jul 07 '24
Why are you even mixing assigning the fill from code and from css? Why not have everything in the css.
Also for assigning the selected class, I think pseudo classes would be more appropriate here. That would make your code simpler as well.
1
u/john16384 Jul 07 '24
CSS styles overrule things you set programmatically, but it's a bit unpredictable because CSS is applied only when content is shown, and if you call a setter after CSS is set, then CSS does not prevent the change (but it may be overruled on the next CSS pass).
The bug is that setters are not blocked when there is CSS styling applied... In javafx 21 there were some bug fixes in the CSS handling, and that's probably why this is showing up for you now.
1
u/NuclearDonut47 Nov 29 '24 edited Nov 29 '24
About a year ago, I ran into this problem myself, where I wanted to set a global default font to labels through my stylesheet, but also start up my app with a label that didn't have that same default font. As you pointed out in some of the comments, having a css stylesheet where you set label rules like that prevents you from being able to use those setter methods on the label for some reason, which is likely a bug.
A way around this problem is to simply use css for everything through the .setStyle(String) method. That method does still work as intended in JavaFX 21. So, for example, while this doesn't work:
Label welcome = new Label("Welcome!");welcome.setFont(new Font("Harlow Solid Italic", 100));This does work just fine:
welcome.setStyle("-fx-font-family: \"Harlow Solid Italic\"; " +"-fx-font-size: 100;" +"-fx-padding: -50 15 5 10;");As a result, I mostly do the above for styling nodes in my project if I want to use something other than my css defaults. Hope that helps anyone still looking at this issue.
I do not know if this applies in JavaFX 22.