Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Can't display UTF-8 encoded text in a JLabel

3,484 views
Skip to first unread message

jgri...@free.fr

unread,
May 14, 2008, 10:02:11 AM5/14/08
to

Hello,

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

unread,
May 14, 2008, 10:47:24 AM5/14/08
to
jgri...@free.fr wrote:
> Hello,
>
> 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
Break the operation into single steps then put it back together - your
new String is taking the UTF-8 byte array and converting it back into
the default charset.

--
Dave Miller
Java Web Hosting at:
http://www.cheap-jsp-hosting.com/

RedGrittyBrick

unread,
May 14, 2008, 10:56:26 AM5/14/08
to
jgri...@free.fr wrote:
> Hello,
>
> 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).

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

Roedy Green

unread,
May 14, 2008, 10:59:36 AM5/14/08
to
On Wed, 14 May 2008 07:02:11 -0700 (PDT), jgri...@free.fr wrote,
quoted or indirectly quoted someone who said :

>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

jgri...@free.fr

unread,
May 15, 2008, 9:34:12 AM5/15/08
to
Your example works for me too ! But unfortunately this is not what I
am trying to do. I got a text string from a UTF-8 encoded file and
after reading the file I set a String with the text content in order
to display in a JLabel.

Roedy Green

unread,
May 15, 2008, 1:59:08 PM5/15/08
to
On Thu, 15 May 2008 06:34:12 -0700 (PDT), jgri...@free.fr wrote,

quoted or indirectly quoted someone who said :

>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

Thomas A. Russ

unread,
May 15, 2008, 12:16:30 PM5/15/08
to
jgri...@free.fr writes:

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

RedGrittyBrick

unread,
May 15, 2008, 2:54:10 PM5/15/08
to

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

jgri...@free.fr

unread,
May 16, 2008, 4:41:30 PM5/16/08
to
OK I admit my example was flawed. The text string really came from a
ressource bundle file (UTF-8 encoded of course) using the standard
ressource bundle API. Anyway many thanks RGB, your last example was
very inspiring.

JGG

susannamoore

unread,
Jun 24, 2014, 6:36:14 AM6/24/14
to
Nice


Roedy Green

unread,
Jun 24, 2014, 11:26:54 PM6/24/14
to
On Tue, 24 Jun 2014 05:36:14 -0500, susannamoore
<susanna...@yahoo.com> wrote, quoted or indirectly quoted someone
who said :

>Nice

Inside a java program you are using UTF-16. SetText wants a UTF-16
string, not a bytes representing a UTF-8 string.
--
Roedy Green Canadian Mind Products http://mindprod.com
Why program by hand in 5 days what you can spend
2 to 5 years of your life automating.
~ Terence Parr 1964-08-17

0 new messages