Is it safe to use Unicode characters in Java GUI? - java

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)

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.

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

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 )

Related

Is it possible to write lambda letter (λ) in IntelliJ IDEA?

I would like to use this interface:
http://javaslang.com/javadoc/2.0.0-RC2/javaslang/%CE%BB.html
Fortunately auto completion works, if I start from package name.
But is it possible to write lamda greek letter in IntelliJ IDEA or generally in any editor?
This is all about your keyboard and OS, and not specific to Intellij IDEA. You can create one by copy and pasting, which is pretty easy. If you want to insert it directly then use the way of typing special characters specific to your platform (Windows, Mac, Linux, ...).
So for example on Mac you could:
Do Edit > Special Characters and click on the gear wheel at the top left, select Customize and check the box for Greek. Double click or drag drop to input.
Go to system prefs/language & text/input sources and check the box for Greek, plus the box for Show Input Menu in Finder. Then select Greek from the "flag" menu at the top right of the screen and type.
There are also some suggestions on the Wikipedia article on entering special characters, though IntelliJ may intercept some of these.
I have to admit, I always just copy and paste.
You can express any Unicode character anywhere in Java source code via the \udddd form. It is not just for Strings and chars. (For characters outside the BMP, you need two of those, forming a surrogate pair.) Inasmuch as there sometimes are multiple characters with similar glyphs, Unicode escapes have the advantage of being totally unambiguous.
Presumably the character wanted in this case is the one Unicode names "Greek small letter lamda", U+03BB, which you can express anywhere in Java source code as \u03bb.

How to use Character.UnicodeBlock while I set some strings in a JEditorpane

I need to display some Bengali characters. I've tried to set the font to a Bengali Unicode font but It does not work properly. The last hope to fulfill my project is to use Character.UnicodeBlock. But I do not have any idea about it. Is it really possible to get the actual display of any Unicode character in Java? How can I use Character.UnicodeBlock in a component?
I assume Java 7.
First one needs a Unicode font.
If this font goes into the Windows fonts, and there is no name clash with an already existing font, everything should work.
Otherwise one might store the font as resource file in the application:
InputStream fontIn = getClass().getResourceAsStream("/.../... .ttf");
Font font = Font.createFont(Font.TRUETYPE_FONT, file);
GraphicsEnvironment.getLocalGraphicsEnvironment().registerFont(font);
After this jEditorPane.setFont(font) should work. Mind, the text in the JEditorPane should not be HTML, where own fonts might be set.
It is tricky, because of font substitution, on font decoding using names.
Another problem might be hard-coded strings in the java source: the encoding of the java source (the editor) must be the same as is used by the javac compiler. For international projects best both UTF-8 (javac -encoding=UTF-8 ...). To test whether there is a problem with that one can test with "\u099C" for জ.

Loading custom fonts at runtime for use with JTextPane

Thanks for your time. My question is regarding the display of different fonts within the one JTextPane. My client wishes to view a word in two different languages within the one field. They've explicitly specified that they wish the different languages (namely Amharic, Arabic, Coptic and Hebrew) to be shown with different fonts. These are obviously non-standard fonts and I can't rely on the user having the required fonts installed on their OS.
From my research I've found that I can load a font file at runtime and set the JTextPane's font accordingly, which is fine if I just wanted to use one font, not two. I've also read about adding fonts to the OS' font directory or the JRE's font directory, outlined here.
I was hoping however, that there might be away to use the fonts without altering the user's OS. Am I out of luck?
Thanks again for your time and I look forward to any replies with bright ideas!
From my research I've found that I can load a font file at runtime and set the JTextPane's font accordingly, which is fine if I just wanted to use one font, not two.
A JTextPane can use multiple fonts.
Check out the section from the Swing tutorial on Text Component Features for an example of playing with the attributes of the text in the text pane.
Edit:
However to use multiple fonts, the only way I have worked out to set the font is by creating a MutableAttributeSet and setting the "FontFamily" attribute (a string) to the desired font name, and then assigning the Attribute set to the text using the StyledDocument.setCharacterAttributes
Reading the API for the createFont() method it looks like you should be able to use:
GraphicsEnvironment.registerFont(Font)

English characters don't show up when entering text with Urdu fonts in Swing

This is similar to my own previous question, but that solution didn't work here.
As mentioned in the previous question, I'm working on a cross platform(Windows/Ubuntu) application that has to transliterate English into one of several official Indian languages. The application has a custom input method, and typing in English and pressing space will transliterate the typed text into the specific local language. Urdu is different from the others in being right to left, like Arabic/Hebrew.
I managed to find an open licensed Urdu font that has both English and Urdu glyphs, but when I type characters in English, nothing shows up.
I don't understand whether it's a font painting issue, or related to the input method. So far, if I disable the custom input method (InputMethod.dispatchEvent() ) for this language, I am able to see the English text (but of course no transliteration takes place).
My findings:
Change font to one of Windows' built in Arabic fonts - same result.
Instead of using ComponentOrientation to align text in the text field, I used setHorizontalAlignment for when the locale is Urdu. Same result.
Decompiled the JDK's default input method provider on Windows (sun.awt.windows.WInputMethod). Here I see the dispatchEvent() makes a native call to the OS for handling IME. I can't do that here.
Found a custom IM for Hebrew - my version of dispatchEvent() is essentially the same.
Stepped through code for JTextField in Eclipse - wasn't able to find anything in the AbstractDocument and subclasses. The AbstractDocument.insertUpdate() method checks for and updates bidirectional text input, but there wasn't anything else significant.
I'm unable to understand what happens after the dispatchEvent() call. The characters are being registered, i.e. the transliteration engine is able to detect the typed characters and process them, but they just don't show up on screen.
Workaround
If I let the text field's orientation be as it is for regular left to right languages, I can see the English text. However, this would not be acceptable to an Urdu speaking user.
Can someone point me in the right direction?
I set the locale to ur_IN.
Sadly, ur_IN is not among the supported locales; I only see en_IN and hi_IN. In the example cited, I used the following code to get the image below:
spinner.setLocale(new Locale("hi", "IN"));

Unicode symbols (arrows) in Java

i want to use following symbols for buttons in my app:
arrows http://img402.imageshack.us/img402/3176/arrowso.jpg
here my code:
Button goToFirstButton = new Button("\uE318");
Button prevPageButton = new Button("\uE312");
Button nextPageButton = new Button("\uE313");
Button goToLastButton = new Button("\uE319");
and the result is
result http://img693.imageshack.us/img693/9063/resultbu.jpg
It seems, that \uE318 and \uE313 are wrong. What should i use instead? For goToLastButton and goToFirstButton i prefer to use this images
alt text http://img3.imageshack.us/img3/5724/singlearrow.jpg
but i can't find, which code should i use.
I would suggest to use Icons on Buttons instead of special characters, because the ability
to display may be strongly affected by availability of fonts on client workstation.
The unicode codepoints you want to use are part of a private use area, i.e. every font manufacturer is free to put whatever character they like at whatever position. The font you used to look up the arrow characters is simply a different font than the one used for displaying the button text. If the button text maps \uE318 and \uE313 to some Chinese (?) graphem, then that's not wrong, just different.
Although multiple people have made the argument you should avoid using these codepoints because you can't rely on the users' systems having a font which displays these characters, the reason you're getting the wrong characters in your example case has been entirely missed. All of the symbols you are trying to draw are in the "private use area" which means that the symbols involved will potentially be different in every single font.
The Unicode standard states:
Private Use Area (E000-F8FF)
* The Private Use Area does not contain any character assignments, consequently no character code charts or namelists are provided for this area.
If you embed the particular font you want to use to insure you can use these codepoints, that's fine. But that does mean you should, indeed, use the \u#### notation in your code, because embedding the characters as Unicode directly means the source won't make sense unless somebody views it in the correct font.
All in all, it's probably better to use icons unless you already have a symbol font you think is simply far superior to any graphical work you would otherwise do.
◀ ▶
There are a couple of symbols close to what you want in the Geometric Shapes chart. However, as others have said, use icons and stay out of the private use area.
Java source files are UTF-8 encoded, so you can put the symbols you want directly in the source code (just copy 'n paste from a font viewer or web), as long as you use a decent editor. No need to use this confusing "\uXXXX" notation. For instance, I've found this useful for Greek letters commonly used in scientific notation (δ, ρ, ψ...) - you can even use them as variable names.
Of course, your font of choice must have the symbols you want, otherwise it won't work.
I would suggest that if you are worried about the client font's ability to display a particular character, use the following:
private static final HashMap<Character, Font> fontCache = new HashMap<Character, Font>();
public static Font getFontThatCanDisplay(char c, int style, float size) {
Font f = fontCache.get(c);
if (f == null) {
f = UIManager.getFont("Label.font");
for (Font font: GraphicsEnvironment.getLocalGraphicsEnvironment().getAllFonts()) {
if (font.canDisplay(c)) {
f = font;
break;
}
}
fontCache.put(c, f);
}
return f.deriveFont(style, size);
}
I also suggest using icons. Not all fonts have symbols for all unicode character points.
You may get it working by specifying a particular font. But what if that font is found on Windows 7 computers, but not on Mac OS X or Linux or Windows XP? Then the system will choose another font, based on the browser defaults, and that default might not have the symbols you want.
I will defend using Unicode glyphs on buttons, but it's really easy to implement the Icon interface and use paintIcon() to draw anything you want. The tutorial "Creating a Custom Icon Implementation" is a good example; this code shows a more complex, animated histogram.

Categories