I'd like to make my application fluid. However, the fonts look small compared to the UI elements when I make the windows bigger. Ultimately, I want the size of my text to get bigger or smaller when I resize the window. I know I could theoretically do this with the style property, but I already have that property bound to something else in some instances.
I know there's the "fontProperty" method, but I have nothing to bind it to since I can't figure out how to create a dynamic ObjectProperty with a synced size. What should I do?
EDIT: To avoid confusion, I'm trying to make the Font size change based on other factors, not the other way around.
There is a simplest and proper method to bind font size with the container size, in order to make it in a responsive font scale effect.
The
bind(Bindings.concat("-fx-font-size: "
is a good alternative but when you resize the windows it seems to be very slow and I think is not the proper way to resolve the problem.You can declare FontProperty and bind this FontProperty to the Component (Label, Text, etc.) and finally create an event to bind the size with the FontProperty size according with our design:
Then in constructor or some init method you can bind the font property of our object with our dynamic font:
Finally, you need to bind the wrapper container change size with the dynamic font size:
With this solution the dynamic font adjustment is very flow and fast, even more than
bind(Bindings.concat("-fx-font-size: "
method.I had a similar problem and solved it this way:
It works because the style that is set to the main pane will affect as a modifier all the nodes that are already defined in the FXML. The nodes that already have a style class are also impacted as the new style comes as an additional top layer of styling in the cascading style sheets.
The main advantage of this solution is to handle in a single place the font size zoom factor. Thus you don't have to find every node that has a Font property to make it working.
Sample files:
styles.css
main.fxml
MainController.java
Set the .root -fx-font-size
In sylesheet
.root
selector, set-fx-font-size
to your desired value:.root { -fx-font-size: 40px; }
Why this works
This works because:
em
units (which are based on the default font size).-fx-font-size
is an inherited style.Once you do this, all controls (and the text inside them) will automatically resize nicely to fit whatever font size you specified.
Related Sample
Related Information
em
is a generic unit that is not specific to JavaFX and em units are also used in HTML CSS. If interested, you can read a broader discussion on em units versus other units.Using em units in FXML via expression binding
Just setting a default font size gets you about 90% of the way to where you need to be, but is not necessarily a universal fix as some layout units might be specified not using em units. Most of the time this isn't really an issue, but if it is in your case, you could also apply a mechanism described in an Oracle developer mailing list post, which appears to work, though is a little clunky.
How about using an expression binding.
For 35em x 25em you could write:
It's not 100% concise, but perhaps passable.
These kind of sizing expressions work in scene builder 1.1, which is nice.
Here is an example using a Rectangle to store the width and height modifiers for the fxml file.
Or instead, you can create your own unit class and use it in your sizing expressions, for example:
Just put everything where you want the fontsize to change in a container and set that style or use bindings if you want.
I'm not sure what your style is already bound to, but you're allowed to set multiple css values in the style string.