17

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.

enter image description here

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?

Atte Juvonen
  • 4,922
  • 7
  • 46
  • 89
  • Which OS? And how does your command line look like? – Christian Hujer Sep 28 '18 at 06:02
  • I'm testing on Ubuntu 16.04. I'm mainly looking for a solution that doesn't require additional actions from the user (although it would also help just to find a command to scale the app. I did try the one you offered in the other thread, `java -Dsun.java2d.uiScale=2.5 -jar filename.jar` which doesn't scale it for me.). – Atte Juvonen Sep 28 '18 at 10:03
  • What is this frame: is it a `JFrame` ? a `JDialog` ? A [mcve] of it could help. A quick and dirty solution would be to always set it to full screen, but I guess you want a cleaner one. – c0der Oct 02 '18 at 13:51
  • If java 9 is not an option, the first thing I would try is increasing the default look-and-feel font size with higher resolution. – c0der Oct 02 '18 at 15:35
  • @c0der It is a `JFrame`. I am using in fact using Java 9, and it's still not scaling. I can't simply increase the font size etc, because I don't want to ruin the look for regular DPI monitors. – Atte Juvonen Oct 02 '18 at 18:09
  • have a look at http://openjdk.java.net/jeps/263 – Sahil Manchanda Oct 05 '18 at 09:49
  • This has the same information as the link in OP. If I understand it correctly, scaling should work already without any additional actions. – Atte Juvonen Oct 05 '18 at 10:23

2 Answers2

7

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

Edwin Buck
  • 69,361
  • 7
  • 100
  • 138
  • 1,5 years after asking the question I finally got this working on my machine. Thanks! I'm not sure why it works now (I tried GDK_SCALE a year ago and it didn't work, but now it works). I updated my JDK to 13.0.2, maybe that's it... something has just been fixed in Java updates in the past 1,5 years? – Atte Juvonen Feb 14 '20 at 11:31
  • 1
    @AtteJuvonen Odds are you tried GDK_SCALE=2 with Java 8. On Linux I never got that to work, and I suspect that some sites documented Java 9's behavior, others copied it, and finally it got copied into documentation assuming Java 8. I did the deep dive a long time ago to figure this one out, being a fan of Netbeans, and wanting to see the code I'm editing (at the same time!) :) – Edwin Buck Feb 14 '20 at 20:38
-1

It looks like you're using Linux. You can use a command line switch

java -Dsun.java2d.uiScale=2 -jar some_application.jar

From https://wiki.archlinux.org/index.php/HiDPI#Java_applications

Charlie
  • 8,530
  • 2
  • 55
  • 53
  • 1
    Unfortunately that doesn't work (this was discussed in the comments in OP already). – Atte Juvonen Nov 29 '18 at 15:58
  • I was seeing a lot of conflicting information that might be dependent on the version; is still not working in JRE 9 and 11? – Charlie Nov 29 '18 at 19:05
  • The linked article also references env var GDK_SCALE, does that work? – Charlie Nov 29 '18 at 19:06
  • It's not working on JRE 9. I haven't tried JRE 11. I tried GDK_SCALE now, but it didn't make a difference either. – Atte Juvonen Dec 02 '18 at 16:17
  • @Charlie This setting temporarily worked for some versions of Java 8 on some platforms. It was never meant to be permanent solution. First it configures a private library (sun.java2d), second the scaling factor only worked with specific toolkits (gtk2, if I recall), and third the setting often would be overridden by the replacement auto-detection code that the GTK_SCALE=2 solution would implement. – Edwin Buck Aug 16 '23 at 13:11