Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to fix scaling of a Java-based application on a high DPI display in Linux?

I just switched to Java 11 (AdoptOpenJDK) so that my Java Swing application looks OK on high DPI displays at different system scaling settings.

It works OK on Windows. Regardless of the scaling value configured on the system, Java automatically detects it and uses it to scale the GUI accordingly.

However, on Linux the GUI does not consider the system scaling, and thus, it looks tiny on high-DPI displays.

After reading some posts here, I found I can indicate Java what scaling value to use. For example, if system scaling is 200%, then I can add the following command line argument to the java command used to launch the application.

-Dsun.java2d.uiScale=2.0

The GUI looks fine on Linux when I add the above command line option.

I also read I can set the GDK_SCALE environment variable.

However, I'd like to find a better solution. Ideally, one where I don't need to specify the scaling value to use. Does anybody know if this is possible?

If the above is not possible, then I guess my next step will be to come up with a command in Linux that returns what the current system scaling value is and use it to set the sun.java2d.uiScale option.

like image 974
Sergio Avatar asked Sep 03 '25 14:09

Sergio


1 Answers

Outside of a set of pre made scales and math scaling for all gui components, you could use borderlayout to size components and gridbaglayout to position components then only fonts and images require math resize.

graphicsdevice and graphicsenvironment classes Re: java.awt.GraphicsConfiguration. getBounds()

like image 168
Samuel Marchant Avatar answered Sep 05 '25 03:09

Samuel Marchant