How can I make my Java Swing app GUI scale properly to users on high-DPI screens?
See the screenshot below. At the top you can see how tiny the app looks compared to Ubuntu's file explorer icons and gedit. In the bottom left you can see roughly what size the app should look like (and does look on regular DPI monitors). So I'm looking for a way to scale the GUI of the app properly when a high DPI monitor is in use. For example, gedit looks the same on both regular DPI and high DPI monitors. I want my app to behave like this.
Here is source code for the app: https://github.com/baobabKoodaa/baopass
This is an extremely common problem affecting many apps. Roughly half of the apps I run on Ubuntu are scaled properly without any actions from the user, the other half are not scaled and look really tiny. Note that I'm mainly looking for a solution that doesn't require actions from the user (although any help is appreciated at this point - I haven't found any ways to scale it at all).
According to this scaling should already work out of the box. It doesn't. I'm not sure if this is a bug or if there is some additional step I'm supposed to do besides running the app on Java 9?
Solution. Go to Compatibility tab and put a check mark next to Disable Display scaling on high DPI settings. Re-launch Java Console and this should resolve the issue.
To access the Screen Resolution preference tool, click Launch, then choose Preferences -> Desktop Preferences -> Display -> Screen Resolution. Use the Screen Resolution preference tool to specify the resolution settings for your screen.
Here’s the fix: 1 Find java.exe and/or javaw.exe likely found in C:Program FilesJavajre (version 2 )bin. 3 Right click on it and select -> Properties 4 Go to Compatibility tab 5 Check Override high DPI scaling behavior. 6 Choose System for Scaling performed by: 7 Click OK More ...
The problem here seems to be that Swing is by default claiming that it is DPI aware, so windows doesn't scale it. Use this switch to turn off this behavior and windows will start scaling your swing app: [EDIT: Unfortunately, this flag no longer seems to work in Java 8, I was testing it in Java 6. Looks like this is a known issue .]
Typically, such apps were written before high DPI displays appeared and were not properly updated to support high DPI. They appear too small on the screen with impossible to read fonts and they don't scale properly. Often buttons are misplaced or too tiny to click for old apps which haven't been updated for high DPI displays.
Find java.exe and/or javaw.exe likely found in C:\Program Files\Java\jre (version#)\bin. Check Override high DPI scaling behavior. Choose System for Scaling performed by: Launch your java app and tada! Thanks to webOS friend, FastbootOEM for the tip!
You have to tell the drawing libraries to scale the app up.
GDK_SCALE=2 ./application
Will have the appropriate information set in the environment and the widgets will render scaled up such that each pixel effectively takes four pixels of footprint.
Note that the splash screen (if you use Java's splash screen support) isn't presented after the entire Swing libraries are loaded, so it won't scale regardless of the settings you attempt.
In some platforms, like the Linux distribution of Fedora, partial scaling is also possible, such that you can set GDK_SCALE=1.5. Just keep in mind that it's not universally available, and it is not settable to any scaling ratio you might want.
Finally, the older JVMs ignore this setting completely, so if you aren't launching with Java 9 or higher, odds are it won't work. And, of course, the way that things are tweaked for older JVMs and different operating systems tend to vary (if they work at all).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With