dcn
dcn

Reputation: 4469

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)

Upvotes: 5

Views: 1408

Answers (3)

Joonas Pulakka
Joonas Pulakka

Reputation: 36577

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.

Upvotes: 6

Иван Бишевац
Иван Бишевац

Reputation: 14641

Most of editors have support for unicode, so go on.

Look at this post: Eclipse French support

If you are using simple editor like notepad then when you save type name and below it choose UTF encoding ( http://www.sevenforums.com/software/72727-how-make-notepad-save-txt-files-unicode.html )

Upvotes: 2

mKorbel
mKorbel

Reputation: 109813

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

Upvotes: 3

Related Questions