I'm currently using FlatLaf UI as look&feel for my Swing application.
FlatLaf offers a user scale mechanism based on the font size, e.g.:
12pt: 1.0 | 100%
16pt: 1.3 | 130%
This means all the borders and insets are calculated considering this factor:
BorderFactory.createEmptyBorder(UIScale.scale(10), ... // Which means 10 * 1.3, for example
The problem arises when the user scale is changed and components are shown on screen.
Given I add and customize components on class constructors...
public class ProtocolSearchBar extends Box {
public ProtocolSearchBar() {
...
add(protocolLabel);
add(Box.createHorizontalStrut(UIScale.scale(10)));
add(protocol);
add(Box.createHorizontalStrut(UIScale.scale(10)));
add(fetchButton);
}
...
...how can I efficiently update their insets each time the UI scale is changed by the user?
FlatLaf offers a callback to be notified.
UIScale.addPropertyChangeListener(event -> {
if ("userScaleFactor".equals(event.getPropertyName())) {
...
}
});