I'm looking to add Hi-DPI support to some Swing applications, but I have failed to find a solution sufficient to my needs. I need to support multiple look & feels, so the situation seems rather more complex than other posts I've found (which tend to suggest "adapt your UI sizes to match your font size").
Some experimentation has found the UIManager
contains many metrics that can be tweaked to get you off to a good start in making an application Hi-DPI friendly. (The UIManager-Defaults utility has been invaluable for exploring these!) What I found though is that the L&Fs seem to work completely differently to each other:
Windows L&F gives you a good (not perfect) default font size, and built-in icons (like checkbox & window icons) are sized appropriately - but many other metrics are still out of whack.
In Metal you can update the fonts in the
UIManager
individually. With a bit of work you can scale the built-inIconUIResource
s to match.In Nimbus you can just update a single default font, and the other fonts fall into place... But it's beyond me how to scale the built-in icons and have combo-boxes, radio buttons (etc.) render successfully!
The feeling I get from playing around is that it should be possible to create a list of specific tweaks for each L&F-specific independently. This would comprise potentially tweaking the defaults for Font
, Icon
, Integer
, and Dimension
s.
Has anyone come up with a good solution to this?
Can anyone share a definitive list of which UIDefaults
need adjustment for the standard L&Fs?
I would be happy with a solution that just supports Metal and Windows consistently.
I imagine such a solution should be quite reusable & could solve the same problem for a range of Swing apps. I'm surprised no such utility yet seems to exist. This approach won't solve everything of course (for example you'd still need to manually scale any calls to setPreferredSize
etc. Then again apps that already support multiple L&Fs should tend to avoid calling that anyway.) Still, I imagine that it could get many apps off to a good start.
I'm aware that JDK-9 promises full Hi-DPI support, but I cannot wait that long - I may not be able to switch for some time even after it's 2017 release.
UPDATE: Yes JDK-9 onward enabled HiDPI support, however the Built-in L&F's at non-integer scaling levels didn't embrace it well & can look even worse than "hack it yourself in Java 8". This problem can be better solved at the L&F level, however, and some excellent 3rd party L&F's like FlatLaF and Radiance do so quite beautifully. Highly recommended.