I am trying to display unicode text in a JLabel, unfortunately the
snippet code right below will fail ... (the JLabel displays it as
ISO-8859-1).
new JLabel(new String("ééééé".getBytes("UTF-8")))
Any idea / suggestions ?
Thanks
Jitou
--
Dave Miller
Java Web Hosting at:
http://www.cheap-jsp-hosting.com/
Your posting is encoded in 8859-1 too. This doesn't help.
>
> new JLabel(new String("ééééé".getBytes("UTF-8")))
>
> Any idea / suggestions ?
Assuming you want a row of e with acute accent, set your IDE or editor
to UTF8 and then type in
------------------------- 8< ------------------------------
public class CharacterEncodingTest {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new CharacterEncodingTest();
}
});
}
CharacterEncodingTest() {
JPanel p = new JPanel();
p.add(new JLabel("ééééé"));
JFrame f = new JFrame("Char Encoding Test");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(p);
f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);
}
}
------------------------- 8< ------------------------------
The above works for me.
If I have my newsreading software set correctly, this posting will be
encoded in UTF-8 and all will be well.
You can always avoid most charset/encoding issues by doing something like
new JLabel("\u00e9\u00e9\u00e9\u00e9\u00e9");
If I have misunderstood your question you'd best spell out in words what
characters you types in, what you expected to see and what you actually saw.
The key really is to make sure your IDE/editor is set to use UTF-8.
--
RGB
>I am trying to display unicode text in a JLabel, unfortunately the
>snippet code right below will fail ... (the JLabel displays it as
>ISO-8859-1).
You don't use UTF-8 in JLabels. You use 16-bit Unicode chars.
How that gets displayed on the screen depends on how smart the font
you chose is at displaying unicode. See
http://mindprod.com/applet/fontshower.html
Before you blame the JLabel or the Font, dump the individual chars in
decimal or hex to make sure they truly are the proper values.
See http://mindprod.com/jgloss/unicode.html
--
Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com
>Your example works for me too ! But unfortunately this is not what I
You must partition your problem.
1. make sure the file truly is encoded it UTF-8. See
http://mindprod.com/jgloss/encoding.html and use the encoding
recogniser.
2. read the UTF-8 file. See http://mindprod.com/applet/fileio.html
3. verify that you indeed have the Unicode chars expected by dumping
them in decimal or hex.
4. make sure the font you are using supports those characters. See
http://mindprod.com/applet/fontshower.html
Do you actually have a text string? Or just an array of bytes? I would
have thought that any string you get back from reading the UTF-8 file
would be a proper Java 16-bit Unicode string. Why isn't it?
How do you get your text string?
--
Thomas A. Russ, USC/Information Sciences Institute
Your Google-fu is too weak! Exercise it more!
--------------------------- 8< ---------------------------------
class CharacterEncodingTest {
static final String FILENAME = "foo.txt";
public static void main(String[] args) {
try {
writeMyUtf8File(FILENAME, "ééééé");
final String text = readMyUtf8File(FILENAME);
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new CharacterEncodingTest(text);
}
});
} catch (IOException e) {
e.printStackTrace();
}
}
static void writeMyUtf8File(String name, String text)
throws IOException {
FileOutputStream fos = new FileOutputStream(name);
Writer osr = new OutputStreamWriter(fos, "UTF-8");
osr.write(text);
osr.close();
}
static String readMyUtf8File(String name) throws IOException {
FileInputStream fis = new FileInputStream(FILENAME);
InputStreamReader isr = new InputStreamReader(fis, "UTF8");
BufferedReader br = new BufferedReader(isr);
String text = br.readLine();
br.close();
return text;
}
CharacterEncodingTest(String text) {
JPanel p = new JPanel();
p.add(new JLabel(text));
JFrame f = new JFrame("Char Encoding Test");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(p);
f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);
}
}
--------------------------- 8< ---------------------------------
--
RGB
JGG