Set the size .root -fx-font-size
- Create a custom style sheet for your application.
In the sylesheet .root selector, set -fx-font-size to the desired value:
.root {-fx-font-size: 40px; }
Why does it work
This works because:
- All default controls are based on
em units (which are based on the default font size). -fx-font-size is an inherited style.- Everything is inherited from the root.
Once you do this, all the controls (and the text inside them) will automatically resize to fit any font size you specify.
Related example
- javafx auto resize and add buttons
Related Information
em is a common element that is not specific to JavaFX, and em units are also used in CSS CSS. If you are interested, you can read a broader discussion of em units compared to other devices .
Using em units in FXML by expression binding
Just setting the default font size gives you about 90% of where you should be, but is not necessarily a universal fix, as some layout units can be specified without using em units. In most cases, this is not a problem, but if it is in your case, you can also apply the mechanism described in the Oracle developers mailing list , which seems to work, albeit a little awkwardly.
How to use expression binding.
During 35x x 25m you can write:
prefWidth="${35*u.em}" prefHeight="${25*u.em}"
This is not 100% concise, but possibly walkable.
These sizing expressions work in Scenario 1.1, which is nice.
Here's an example of using a Rectangle to store width and height modifiers for an fxml file.
<?xml version="1.0" encoding="UTF-8"?> <?import javafx.scene.control.*?> <?import javafx.scene.layout.*?> <?import javafx.scene.shape.*?> <StackPane xmlns:fx="http://javafx.com/fxml"> <fx:define> <Rectangle fx:id="s" width="13" height="13"/> </fx:define> <AnchorPane id="AnchorPane" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="${22 * s.width}" prefWidth="${14 * s.height}"> <children> <Button layoutX="${4 * s.width}" layoutY="${ 5 * s.height}" prefWidth="${6 * s.width}" text="Top" /> <Button layoutX="${4 * s.width}" layoutY="${10 * s.height}" prefWidth="${6 * s.width}" text="Middle" /> <Button layoutX="${4 * s.width}" layoutY="${15 * s.height}" prefWidth="${6 * s.width}" text="Bottom" /> </children> </AnchorPane> </StackPane>
Or instead, you can create your own class of units and use it in your size expressions, for example:
package org.jewelsea.measure; public class Measurement { private double em; public void setEm(double em) { this.em = em; } public double getEm() { return em; } } . . . <?xml version="1.0" encoding="UTF-8"?> <?import javafx.scene.control.*?> <?import javafx.scene.layout.*?> <?import org.jewelsea.measure.Measurement?> <?scenebuilder-classpath-element .?> <StackPane xmlns:fx="http://javafx.com/fxml"> <fx:define> <Measurement fx:id="u" em="26.0" /> </fx:define> <AnchorPane id="AnchorPane" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="${22*u.em}" prefWidth="${14*u.em}"> <children> <Button layoutX="${4*u.em}" layoutY="${ 5*u.em}" prefWidth="${6*u.em}" text="Top" /> <Button layoutX="${4*u.em}" layoutY="${10*u.em}" prefWidth="${6*u.em}" text="Middle" /> <Button layoutX="${4*u.em}" layoutY="${15*u.em}" prefWidth="${6*u.em}" text="Bottom" /> </children> </AnchorPane> </StackPane>