Search code examples
javaswingcharacter-encodingjlabeloff-by-one

JLabel text is garbled


I'm seeing a strange issue involving a JLabel that is used to display a range of numbers. The text of the label is typically something like 0.0 - 100.0 (for example). The problem is that for a select few users, the text appears garbled. In this example, the text would show up as /-/,0//-/.

Here's what I know so far:

  • It appears to be some sort of off-by-one bug since each character displayed is one Unicode character behind the expected character.
  • The dash character between the two numbers is hard-coded in the application as a string literal.
  • It's happening on various versions of Oracle JVMs including 1.6.x and 1.7.x.
  • It's happening on a few select Windows 7 machines.

What could cause this type of problem? What else should I investigate? Thanks.


Solution

  • This happens when you're setting the font of your JLabel to a font corrupted in the Windows font cache.

    To rebuild the cache: as administrator, delete this file and reboot the PC:

    del %WINDIR%\System32\FNTCACHE.DAT