I want to read the background color of a control from javaFX code and then apply a transparency to it. I have been looking for information but I am not clear. There is no method getBackgroundColor()
but if getBackground()
although I'm lost, any idea?
See if this is what you need in this link: window with shaded edges
The part to watch carefully is:
Y
Sorry for the kilometric answer, I hadn't used JavaFX for a long time and for that reason I investigated a little more thoroughly.
Short answer:
You can get the colors used for the background from the method
getBackground
, which itself has a list of the colors used inside, and unless you're using a gradient or something similar, the first fill should be the unique background color of your object.Long answer:
It is important that you differentiate between the color that is set programmatically and the one that is set from the javafx-css style , since the styles have a higher preference than the normal background colors and can produce subtle errors, which may or may not work in our favor and I always suggest using programmatic colors instead of javafx css styles.
Let's see a simple example, if you have an fxml with a button like this:
and in your button handles you poes something like:
The first time you run it you will get a very horrible NPE (Not what you think naughty ;) ) like this:
Now if we add a color selector to our fxml
And in our Java code
We will notice that the color only changes temporarily and returns to the color defined in the style.
That is why I recommend that if you need to modify the color, do it directly in the style property.
FXML:
Java:
If you happen to be doing color binding, I recommend you read this question in English .
If you use transparency you can try in the css with something like:
or if you are directly modifying the Background from code by setting the fills to null.