Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it safe to use Unicode characters in Java GUI?

For a play button in a Java GUI I currently use a button with the label set to ' ▻ ' (found this symbol in a Unicode symbol table). As I understand, it is better to not use such symbols directly in source code but rather use the explicit unicode representation like \u25BB in this example, because some tools (editor, ...) might not be able to handle files with non-ASCII content (is that correct?).

Assuming the compiled class contains the correct character, under which circumstances would the GUI not show the intended symbol on a current PC operating system? Linux, Windows, Mac should all support UTF-16, right? Do available fonts or font settings cause problems to this approach?

(Of course I could add an icon, but why add extra resources if a symbol should already be available... given that this is a portable solution)

like image 262
dcn Avatar asked Sep 13 '11 09:09

dcn


People also ask

Does Unicode work in Java?

Unicode sequences can be used everywhere in Java code. As long as it contains Unicode characters, it can be used as an identifier.

How does Unicode work?

Unicode uses two encoding forms: 8-bit and 16-bit, based on the data type of the data that is being that is being encoded. The default encoding form is 16-bit, where each character is 16 bits (2 bytes) wide. Sixteen-bit encoding form is usually shown as U+hhhh, where hhhh is the hexadecimal code point of the character.


2 Answers

Do available fonts or font settings cause problems to this approach?

Unfortunately they do. You can use unicode in the source code of course, but the problem is that currently unicode has 246,943 code points assigned so obviously no font has even a fraction of those defined. You'll get squares or some other weird rendering when the glyph isn't available. I've had cases where relatively simple symbols such as ³ render fine on one Windows computer and show up as squares in the next, almost identical computer. All sort of language and locale settings and minor version changes affect this, so it's quite fragile.

AFAIK there are few, if any, characters guaranteed to be always available. Java's Font class has some methods such as canDisplay and canDisplayUpTo, which can be useful to check this at runtime.

Instead of using icons, you could bundle some good TrueType font that has the special characters you need, and then use that font everywhere in your app.

like image 64
Joonas Pulakka Avatar answered Nov 06 '22 18:11

Joonas Pulakka


I currently use a button with the label set to ' ▻ ' 

rather than I always use JButton(String text, Icon icon), and Icon doesn't matter if is there this Font or another Font, UTF-16 or Unicode

like image 22
mKorbel Avatar answered Nov 06 '22 19:11

mKorbel