2

I am making some software for my university. A GUI is required. In the first revision of it, I had it use the System Look and Feel (so it would look like a native application within Linux/Mac/Windows). I found this to be cumbersome as I had to make all of my JLabels different sized based on the OS (regardless of resolution/pixel density/etc).

After doing this I was like "okay, I'm NOT going to go through with this again" so I decided to not use the System look and feel and try out Nimbus. It looks nice on the platforms I've tested but the JLabel/JTextField font sizes still don't appear correctly on multiple platforms, regardless of them being specified (as Sans Serif 12). Is there a way I could make the Font truly universal across all platforms and avoid having to do all of this cross-platform testing?

mKorbel
  • 109,525
  • 20
  • 134
  • 319
snotyak
  • 3,709
  • 6
  • 35
  • 52
  • As a sidenote:If you are targeting mostly on Windows, I would recommend you stick with the System L&F.All professional Swing applications try to look as Windows applications and the rest of L&F look weird to most users on Windows. – Cratylus Aug 16 '12 at 19:54
  • The actual look and feel settings aren't so important. I just want it to be completely easily usable without having to make too many changes for different platforms. This software is only going to be for ~20 people so I don't really have a specific target. The question above, however, is pretty general and will help me with future cross-platform development. – snotyak Aug 16 '12 at 20:03

3 Answers3

3

The problem you are facing is the fact that each platform is independent of each other. From memory (and this is going WAY back now), Java renders it's UI at 72 dpi (this could have changed recently).

You are never going to get the same result from one platform to another. This comes down to the difference in individual platform DPI settings.

This is also the same reason why web pages look different from platform to platform and why many applications look slightly different.

The best you can is careful plan your UIs and make clever use of Swings internal metrics, rather then setting the sizes directly.

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • This is unfortunate but it's realistic. Asking for a one-size-fits-all solution for running a single application across vastly different solutions is rather naive, but seeing as Java touts being "cross platform" makes it seem more plausible. I guess my best bet would be to either develop unique versions for OSX, Linux, and Windows or just add some simple control statements to setup font sizes based on the current platform being used. I'm optimistic so I'll hope for other answers, though I'm nearly certain you're correct. Thanks – snotyak Aug 16 '12 at 20:47
  • I'd go for the second option (setup base fonts based n platform),you can use the UIManger to achieve this, however, even across the same platform you will run into differences, what looks great on my 30" screen at home running something like 120dpi, looks crap on my 22"/92 dpi screen at work ;) – MadProgrammer Aug 16 '12 at 21:18
2

The main thing you can do is use a specific TrueType font and bundle it as a resource in your application. This way you get the best chance of the font appearing the same on all platforms. There's no guarantee that "sans serif" will be the same on all platforms - Java will just use the closest available match.

You can then load the the font from your .jar at runtime with something like:

InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream(filename)
Font font = Font.createFont(Font.TRUETYPE_FONT, is);

As a secondary point, it is always worth designing your app layout so that there is a reasonable amount of flexibility on font size. Maybe you just need to make a bit more space for your JLabels, or have text wrap in certain places etc.

mikera
  • 105,238
  • 25
  • 256
  • 415
2

As shown here, using an appropriate layout manager and pack() will allow a component to leverage it's preferred size, regardless of platform and font.

image

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045