Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / GUI / May 2008

Tip: Looking for answers? Try searching our database.

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

Thread view: 
jgricourt@free.fr - 14 May 2008 15:02 GMT
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 - 14 May 2008 15:47 GMT
> Hello,
>
[quoted text clipped - 9 lines]
>
> 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.

Signature

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

RedGrittyBrick - 14 May 2008 15:56 GMT
> 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.

Signature

RGB

jgricourt@free.fr - 15 May 2008 14:34 GMT
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.
Thomas A. Russ - 15 May 2008 17:16 GMT
> 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.

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?

Signature

Thomas A. Russ,  USC/Information Sciences Institute

Roedy Green - 15 May 2008 18:59 GMT
>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.

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
Signature


Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com

RedGrittyBrick - 15 May 2008 19:54 GMT
> 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.

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< ---------------------------------

Signature

RGB

jgricourt@free.fr - 16 May 2008 21:41 GMT
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
Roedy Green - 14 May 2008 15:59 GMT
>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
Signature


Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.