26

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.

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Sergio
  • 311
  • 1
  • 4
  • 7
  • It looks like it is related to this issue: https://bugs.openjdk.java.net/browse/JDK-8260270 – Miroslav Feb 05 '21 at 18:13
  • This is not a java problem. It is a problem of "the" linux display manager. I have also a high dpi display, that caused issues in the size of guis. Cos I only use xorg, I made it with `Xft.dpi: 140` in the `.Xresources` (xrandr did not work) Former issues with hdpi should be solved https://openjdk.java.net/jeps/263 –  Sep 20 '21 at 20:43
  • It is a java problem - I have yet to see a java program that reacts to Xft.dpi, while almost every other application does the right thing with it. – Remember Monica Jul 23 '22 at 07:39

3 Answers3

1

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()

Samuel Marchant
  • 331
  • 2
  • 6
0

You can use third party tool named jHiccup. It can automatically detects the scaling value of the system. Then, using it you can fix the GUI.

For example,

To get the scaling value

double dpiScaling = Toolkit.getDefaultToolkit().getScreenResolution() / 96.0;

Set the scaling factor

JHiccup.setScaleFactor(dpiScaling);

You can check out the jHiccup's documentation.

Reference - https://docs.azul.com/prime/jHiccup

-4

You can try this one:

-Dsun.java2d.ddscale=true

As the official documentation says:

Setting this flag to true enables hardware-accelerated scaling if the DirectDraw/Direct3D pipeline is in use. DirectDraw/Direct3D hardware scaling is disabled by default to avoid rendering artifacts in existing applications.

For more information, you can see the official Oracle documentation about system properties.

Stephen Ostermiller
  • 23,933
  • 14
  • 88
  • 109