Swing - JButton and Unicode

Hi,

sorry if this is too easy. I want to label my JButtons with Unicode code points and am having trouble doing it. The code is something like this

JButton button = new JButton();

button.setFont(new Font("Arial Unicode MS", Font.PLAIN, 11));

button.setText("\u0654");

The corresponding unicode symbol doesnt display, however. The Arial Unicode MS font is definitely on the system...Any ideas where i'm going wrong?

Thanks

[465 byte] By [DumDuma] at [2007-11-26 17:22:18]
# 1

- write a property file with normal characters like 'o?, 閠? 閠ag鑢e,..."

- run native2ascii program (http://java.sun.com/j2se/1.5.0/docs/tooldocs/solaris/native2ascii.html)

- load the property file in a map and when you wanna display a text, do:

button.setText(myMap.get("jButtonText"));

suparenoa at 2007-7-8 23:50:16 > top of Java-index,Java Essentials,Java Programming...
# 2
But what's that got to do with trying to simply display a quotation mark?OP: show your code - lettus have a look.
abillconsla at 2007-7-8 23:50:16 > top of Java-index,Java Essentials,Java Programming...
# 3

Thanks for responding.

The code is as I've posted it.

JButton button = new JButton();

button.setFont(new Font("Arial Unicode MS", Font.PLAIN , 20));

button.setText("\u0615");

I want the JButtons in the application to have phonetic characters as labels. I'm not sure I fully understand the suggestion regarding the property file. Is it not possible to use Unicode codepoints to directly label the button?

Thanks

DumDuma at 2007-7-8 23:50:16 > top of Java-index,Java Essentials,Java Programming...
# 4

> Thanks for responding.

>

> The code is as I've posted it.

>

>JButton button = new JButton();

> button.setFont(new Font("Arial Unicode MS",

> Font.PLAIN , 20));

>button.setText("\u0615");

> he JButtons in the application to have phonetic

> characters as labels. I'm not sure I fully understand

> the suggestion regarding the property file. Is it not

> possible to use Unicode codepoints to directly label

> the button?

>

> Thanks

Yes it is, it works for me. That's why I asked for code. You posted a code snippet. Let's see a working model.

abillconsla at 2007-7-8 23:50:16 > top of Java-index,Java Essentials,Java Programming...
# 5

Sorry :)

public static void main(String[]args)

{

JFrame environmentFrame = new JFrame();

JPanel symbolPanel = new JPanel();

JButton button = new JButton();

button.setFont(new Font("Arial Unicode MS", Font.BOLD, 14));

button.setText("\u0615");

symbolPanel.add(button);

environmentFrame.add(symbolPanel);

environmentFrame.pack();

environmentFrame.setVisible(true);

}

Thanks again

DumDuma at 2007-7-8 23:50:16 > top of Java-index,Java Essentials,Java Programming...
# 6

To find out if a font can display a char, you can test first with Font's canDisplay method. Try running the following to see if any font can display that character:

import java.awt.*;

public class Test {

public static void main(String[]args) {

GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();

Font[] fonts = ge.getAllFonts();

char ch = '\u0615';

for(Font font : fonts) {

if (font.canDisplay(ch)) {

System.out.println(font.getName());

}

}

}

}

DrLaszloJamfa at 2007-7-8 23:50:16 > top of Java-index,Java Essentials,Java Programming...
# 7
What do you expect to see? U+0615 is not a letter or number, it's a "coranic annotation sign" that I guess only makes sense when it's with other characters, similar to combining diacritic marks. You can check it yourself at http://www.unicode.org/charts/
jsalonena at 2007-7-8 23:50:16 > top of Java-index,Java Essentials,Java Programming...
# 8
coranic?
DrLaszloJamfa at 2007-7-8 23:50:16 > top of Java-index,Java Essentials,Java Programming...
# 9

The things I'm expecting to see form part of the International Phonetic Alphabet. The font "Arial Unicode MS" supports them, and the Unicode codepoints I'm using are correct. When I do the suggested test to see what fonts can display the characters, the Arial Unicode doesn't show up. But it is definitely on the system and other applications can use it. In fact, when I display the Unicode codepoints as part of a HTML document within a JEditorPane, they all display perfectly. So why won't the JButton display them?

DumDuma at 2007-7-8 23:50:16 > top of Java-index,Java Essentials,Java Programming...
# 10
Koranic. Sorry, didn't realize that in English it's spelled with a k.
jsalonena at 2007-7-8 23:50:16 > top of Java-index,Java Essentials,Java Programming...
# 11

You have the wrong code; IPA characters range U+0250 through U+02AF. See the charts for details http://www.unicode.org/charts/symbols.html

Edit: It looks like you have confused decimal notation with hexadecimal notation. The decimal number 615 is 267 in base 16, and U+0267 happens to be one of the IPA characters. This means that these two lines are equivalent:char c = 615;

char c = '\u0267';

jsalonena at 2007-7-8 23:50:16 > top of Java-index,Java Essentials,Java Programming...
# 12
Thanks for all your help. I was aware I was using the decimal notations, I just thought that they were escaped in a similar fashion (i.e. \u0665)Will JButton only accept Unicode characters that are in hex notation, or can one use the decimal ones also?Thanks again
DumDuma at 2007-7-8 23:50:16 > top of Java-index,Java Essentials,Java Programming...
# 13

> Will JButton only accept Unicode characters that are

> in hex notation, or can one use the decimal ones

> also?

JButton doesn't give a rat's posterior how you wrote your Java code. It just gets a String, which is a sequence of characters. Those characters could be constants that you typed into Java code or they could be read from a file or a database. It makes no difference to JButton where they came from, in fact it has no way of knowing that. It just has a String.

In the example that jsalonen just posted, the two lines of code are equivalent. That means they have the same effect. And the resulting character isn't in hex notation, and it isn't a decimal number. It's just a character that can be represented in several ways in Java code.

DrClapa at 2007-7-8 23:50:16 > top of Java-index,Java Essentials,Java Programming...
# 14
there is no decimal notation, only hexbut you can cast any int to a char so you can do things likeprint("Here's the mystery char: " + (char) 615 + ".");
jsalonena at 2007-7-8 23:50:16 > top of Java-index,Java Essentials,Java Programming...
# 15

> there is no decimal notation, only hex

Actually that's a misstatement as the unicode escapes \uXXXX are not specific to literal strings or characters, you can use them anywhere in the code.

The only notation there is for literal char constants is of the form 'x' where x is either one character (unicode-escaped or not) or one of the two-character escape codes \\, \r, \n, \', etc.

jsalonena at 2007-7-21 17:02:27 > top of Java-index,Java Essentials,Java Programming...