f



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

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("=E9=E9=E9=E9=E9".getBytes("UTF-8")))

Any idea / suggestions ?

Thanks

Jitou
0
jgricourt
5/14/2008 2:02:11 PM
comp.lang.java.gui 8286 articles. 0 followers. Post Follow

10 Replies
3864 Views

Similar Articles

[PageSpeed] 26

jgricourt@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/
0
Dave
5/14/2008 2:47:24 PM
jgricourt@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
0
RedGrittyBrick
5/14/2008 2:56:26 PM
On Wed, 14 May 2008 07:02:11 -0700 (PDT), jgricourt@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
0
Roedy
5/14/2008 2:59:36 PM
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.

0
jgricourt
5/15/2008 1:34:12 PM
jgricourt@free.fr writes:

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

-- 
Thomas A. Russ,  USC/Information Sciences Institute
0
tar
5/15/2008 4:16:30 PM
On Thu, 15 May 2008 06:34:12 -0700 (PDT), jgricourt@free.fr wrote,
quoted or indirectly quoted someone who said :

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

Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com
0
Roedy
5/15/2008 5:59:08 PM
jgricourt@free.fr wrote:
> 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< ---------------------------------


-- 
RGB
0
RedGrittyBrick
5/15/2008 6:54:10 PM
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
0
jgricourt
5/16/2008 8:41:30 PM
Nice
1
6/24/2014 10:36:04 AM
On Tue, 24 Jun 2014 05:36:14 -0500, susannamoore
<susannamoore53@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
Roedy
6/25/2014 3:26:54 AM
Reply:

Similar Artilces:

lynx 'browser' can't do "<?xml version="1.0" encoding="utf-8"?>"
lynx is lighteningly fast to fetch a set of pages [via a script] via dump > fileID. But [my current version] doesn't render the 'style' of: "<?xml version="1.0" encoding="utf-8"?>" What is this new "xml" crap ? Will it likely take over the older *.html ? Thanks for any info, == Chris Glur. news@absamail.co.za wrote in news:1172596265.663611@vasbyt.isdsl.net: > lynx is lighteningly fast to fetch a set of pages [via a script] via > dump > fileID. > > But [my current version] doesn't ...

Why does makeindex differentiate between =?utf-8?Q?`=5Cindex{El?= =?utf-8?Q?=C3=A9ment=40=C3=89l=C3=A9ment}'?= and =?utf-8?Q?`=5Cindex{El?= =?utf-8?Q?=C3=A9ment=40=5C'El=C3=A9ment}'=3F?= Okay, t
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Hi, I am using makeindex, and I realized, some days ago, that it differentiates (i.e. make a distinction) between `\index{Elément@Élément}' and `\index{Elément@\'Elément}'. In the output, everybody will agree that either `\'Elément' or `Élément' will produce the same result. Then, why are they written apart if both are typed as index words? I would understand it if there was a practical (i.e. lexicographical) interest to it, but there is no such interest. Thanks. - -- ...

Can't display Java gui
Hi. I'm using sendmail 8.12.9 for a project on RedHat Fedora Core 2 on my personal computer with a perfectly functioning X window server. Part of this project requires me to modify sendmail to open a Java gui application. When I attempted this originally, I got the following error: "No X11 DISPLAY variable was set, but this program performed an operation which requires it." Figuring that sendmail for whatever reason unset it, I called the C function setenv() in sendmail to set it and got the following error: "Exception in thread "main" java.lang.InternalError: Can...

svn: Can't create a converter from 'UTF-8' to 'native'
I have subversion on sun solaris. First, I need to create a respository as below: svnadmin create /usr/local/repos --fs-type fsfs and I got an error as below: svnadmin create /usr/local/repos --fs-type fsfs I have been looking for book, intenet and the web site help http://subversion.tigris.org/. But I don't have any clues. Please help. Thanks in advance. ...

=?UTF-8?Q?ALEX_JONES_=28breaking=29_=E2=80=94=E2=80=94=3E_US__'B?= =?UTF-8?Q?acked_Plan_To_Launch_Chemical_We?= =?UTF-8?Q?apon_Attack_On_Syria=2C_Blame_It_O?= =?UTF-8?Q?n_Assad_Gov't?=
ALEX JONES POINTS TO YAHOO AS CONFIRMATION: US 'Backed Plan To Launch Chemical Weapon Attack On Syria, Blame It On Assad Gov't' http://in.news.yahoo.com/us-backed-plan-launch-chemical-weapon-attack-syria-045648224.html London, Jan 30 (ANI): The Obama administration gave green signal to a chemical weapons attack plan in Syria that could be blamed on President Bashar al Assad's regime and in turn, spur international military action in the devastated country, leaked documents have shown. A new report, that contains an email exchange between two senior offi...

=?UTF-8?Q?ALEX_JONES_=28breaking=29_=E2=80=94=E2=80=94=3E_US__'B?= =?UTF-8?Q?acked_Plan_To_Launch_Chemical_We?= =?UTF-8?Q?apon_Attack_On_Syria=2C_Blame_It_O?= =?UTF-8?Q?n_Assad_Gov't?=
ALEX JONES POINTS TO YAHOO AS CONFIRMATION: US 'Backed Plan To Launch Chemical Weapon Attack On Syria, Blame It On Assad Gov't' http://in.news.yahoo.com/us-backed-plan-launch-chemical-weapon-attack-syria-045648224.html London, Jan 30 (ANI): The Obama administration gave green signal to a chemical weapons attack plan in Syria that could be blamed on President Bashar al Assad's regime and in turn, spur international military action in the devastated country, leaked documents have shown. A new report, that contains an email exchange between two senior offi...

=?UTF-8?Q?ALEX_JONES_=28breaking=29_=E2=80=94=E2=80=94=3E_US__'B?= =?UTF-8?Q?acked_Plan_To_Launch_Chemical_We?= =?UTF-8?Q?apon_Attack_On_Syria=2C_Blame_It_O?= =?UTF-8?Q?n_Assad_Gov't?=
ALEX JONES POINTS TO YAHOO AS CONFIRMATION: US 'Backed Plan To Launch Chemical Weapon Attack On Syria, Blame It On Assad Gov't' http://in.news.yahoo.com/us-backed-plan-launch-chemical-weapon-attack-syria-045648224.html London, Jan 30 (ANI): The Obama administration gave green signal to a chemical weapons attack plan in Syria that could be blamed on President Bashar al Assad's regime and in turn, spur international military action in the devastated country, leaked documents have shown. A new report, that contains an email exchange between two senior offici...

mhshow can't read UTF-8 text
If I receive mail with utf-8 text parts to it mhshow gives up with this: mhshow: don't know how to decode content (content text/plain in message 53) My locale settings are: LANG=en_GB LC_CTYPE="en_GB.UTF-8" LC_NUMERIC="en_GB.UTF-8" LC_TIME="en_GB.UTF-8" LC_COLLATE="en_GB.UTF-8" LC_MONETARY="en_GB.UTF-8" LC_MESSAGES="en_GB.UTF-8" LC_PAPER="en_GB.UTF-8" LC_NAME="en_GB.UTF-8" LC_ADDRESS="en_GB.UTF-8" LC_TELEPHONE="en_GB.UTF-8" LC_MEASUREMENT="en_GB.UTF-8"...

Emacs can't edit files encoded utf-8
Out of the blue, while editing a series of files, all of which had as their first line: <?xml version="1.0" encoding="UTF-8"?>, emacs23 decided it could not load the file: Debugger entered--Lisp error: (wrong-type-argument number-or-marker-p nil) font-lock-fontify-keywords-region(1 45 t) font-lock-default-fontify-region(1 45 t) font-lock-fontify-region(1 45 t) byte-code("ŠÃ ĎÅed #ˆÆ ˆÇ‰+‡" [save-match-data-internal verbose font-lock-fontified match-data ((byte-code "ÁÂ\"‡" [save-match-data-intern...

Why my JAVA based GUI can't display after complied to EXE?
I creat a JAVA based GUI in M file. It works very well in MATLAB. But the GUI windiow does not appear after I compiled it to an EXE. Who can tell me WHY and HOW to fix this problem? Thank you VERY much! Do you have the current version of MATLAB? Have you asked MathWorks support? Stuart "junziyang" <junziyang@126.com> wrote in message news:eef4520.-1@webx.raydaftYaTP... > I creat a JAVA based GUI in M file. It works very well in MATLAB. But > the GUI windiow does not appear after I compiled it to an EXE. Who > can tell me WHY and HOW to fix this problem? Thank you VER...

=?utf-8?Q?BREAKING_NEWS_=E2=80=94=E2=80=94=3E_'ERASE_ISRAEL_?= =?utf-8?Q?FROM_INTERNET'__\__Israel_P.M._N?= =?utf-8?Q?etanyahu_Announces_Israel's_Plan?= =?utf-8?Q?_To_'WIPE'_Lebanon_From_The_Map?=
Two (2) Stories Below: 1. Israel P.M. Netanyahu Announces Israel's Plan To 'WIPE' Lebanon From The Map: ——> PRECIPITATE ARMAGEDDON <—— 2. 'Erase Israel From The Internet': Anonymous Plots Massive Cyber-Attack “ Yes, and it is not a secret that it will happen with U.S.-Gulf support and that is why they have been warned, but before you ask, you have a look at the new map of the world and see that there is no nation (Lebanon) with this name. ” ...

=?utf-8?Q?BREAKING_NEWS_=E2=80=94=E2=80=94=3E_'ERASE_ISRAEL_?= =?utf-8?Q?FROM_INTERNET'__\__Israel_P.M._N?= =?utf-8?Q?etanyahu_Announces_Israel's_Plan?= =?utf-8?Q?_To_'WIPE'_Lebanon_From_The_Map?=
Two (2) Stories Below: 1. Israel P.M. Netanyahu Announces Israel's Plan To 'WIPE' Lebanon From The Map: ——> PRECIPITATE ARMAGEDDON <—— 2. 'Erase Israel From The Internet': Anonymous Plots Massive Cyber-Attack “ Yes, and it is not a secret that it will happen with U.S.-Gulf support and that is why they have been warned, but before you ask, you have a look at the new map of the world and see that there is no nation (Lebanon) with this name. ” ...

=?utf-8?Q?BREAKING_NEWS_=E2=80=94=E2=80=94=3E_'ERASE_ISRAEL_?= =?utf-8?Q?FROM_INTERNET'__\__Israel_P.M._N?= =?utf-8?Q?etanyahu_Announces_Israel's_Plan?= =?utf-8?Q?_To_'WIPE'_Lebanon_From_The_Map?=
Two (2) Stories Below: 1. Israel P.M. Netanyahu Announces Israel's Plan To 'WIPE' Lebanon From The Map: ——> PRECIPITATE ARMAGEDDON <—— 2. 'Erase Israel From The Internet': Anonymous Plots Massive Cyber-Attack “ Yes, and it is not a secret that it will happen with U.S.-Gulf support and that is why they have been warned, but before you ask, you have a look at the new map of the world and see that there is no nation (Lebanon) with this name. ” ...

=?utf-8?Q?BREAKING_NEWS_=E2=80=94=E2=80=94=3E_'ERASE_ISRAEL_?= =?utf-8?Q?FROM_INTERNET'__\__Israel_P.M._N?= =?utf-8?Q?etanyahu_Announces_Israel's_Plan?= =?utf-8?Q?_To_'WIPE'_Lebanon_From_The_Map?=
Two (2) Stories Below: 1. Israel P.M. Netanyahu Announces Israel's Plan To 'WIPE' Lebanon From The Map: ——> PRECIPITATE ARMAGEDDON <—— 2. 'Erase Israel From The Internet': Anonymous Plots Massive Cyber-Attack “ Yes, and it is not a secret that it will happen with U.S.-Gulf support and that is why they have been warned, but before you ask, you have a look at the new map of the world and see that there is no nation (Lebanon) with this name. ” ...

Web resources about - Can't display UTF-8 encoded text in a JLabel - comp.lang.java.gui

Resources last updated: 2/5/2016 1:31:49 PM