Results 1 to 3 of 3
  1. #1
    lumpy is offline Member
    Join Date
    Feb 2010
    Rep Power

    Default Problem displaying UNICODE characters on a JLabel

    Hi everyone,

    I am developing a small "educative" software that displays chinese characters on a JLabel.

    Since I want to display a lot (only one at a time), I downloaded a TEXT file which contains the list of about 7'000 unicode for chinese characters. The file is basically just 1 column and ~7'000 rows and looks like this :

    (Beginning of the file)


    (End of the file)

    My java program reads the file and loads all the unicodes in a vector. Then, what I do is that I create a new String and put somehow the concatenation of "\u" and a 4-digits unicode like for instance "4E00". The result of my concatenation is therefore "\u4E00". Finally, I put the result in my JLabel with the "setText" method.

    It looks like this:

    Java Code:
    jLabelChar.setFont(new Font("SimSun",Font.PLAIN, 12)); // Sets an appopriate font to display chinese characters to the JLabel
    String s1 = "\\u";
    String s2 = getElement("D2B5"); // Will return the 4-digits unicode "4E00" in the vector  as a String, according to a key (the key here is D2B5)
    String s  = s1.concat(s2);
    When I run my program, why does Java display "\u4E00" in the JLabel instead of displaying the corresponding chinese character ?

    Of course, if I just do like this...

    Java Code:
    ...that works, but that's not what I want since I want to be able to take this unicode from a file.

    Could anyone help me ? Try to be accurate as you can in your answer.

    Thanks in advance for your help!

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Blog Entries
    Rep Power


    When I run my program, why does Java display "\u4E00" in the JLabel instead of displaying the corresponding chinese character ?
    Because you are passing \\uXXXX to the String which Java sees as a String, not a char, whereas \uXXXX is seen as a char not a String.

    For instance, try this to see the difference:
    Java Code:
          String s1 = "\u0050";
          String s2 = "\\u0050";
          JOptionPane.showMessageDialog(null, "s1: " + s1 + "\n" + "s2: " + s2);

    Why not use a StringBuilder to build your String. In particular look at its appendCodePoint method.
    For example (using arabic fonts since I have these on my computer):
    Java Code:
    import java.awt.ComponentOrientation;
    import java.awt.Font;
    import javax.swing.*;
    public class SwingFu1 {
       private static final int MAX = 100;
       private static void createAndShowUI() {
          StringBuilder sb = new StringBuilder();
          for (int i = 0; i < MAX; i++) {
             int value = 0x680 + i;
             if ((value + 1) % 10 == 0) {
                sb.append(" ");
          JTextArea area = new JTextArea(10, 30);
          area.setFont(new Font("Arabic Typesetting", Font.PLAIN, 32));
          JFrame frame = new JFrame("SwingFu1");
          frame.getContentPane().add(new JScrollPane(area));
       public static void main(String[] args) {
          java.awt.EventQueue.invokeLater(new Runnable() {
             public void run() {
    Also you may wish to look up the String and Character classes and search on the term "codePoint" which will lead you to solutions using these classes.

    One aside,
    Try to be accurate as you can in your answer.
    This statement probably isn't necessary and may not be appreciated. Help here is free, and anyone attempting to help you is doing so voluntarily and will likely try to be as accurate as they possibly can with or without this request.
    Last edited by Fubarable; 02-15-2010 at 06:18 AM.

  3. #3
    lumpy is offline Member
    Join Date
    Feb 2010
    Rep Power


    Thanks for your reply Fubarable.

    First, sorry for the last sentence, I didn't mean to offend. It's probably not necessary, you're right.

    That works with StringBuilder and the appendCodePoint method, thanks!

    But since this method wants an "int", is there someway to convert a string representation of a unicode (e.g. "4E00") directly to an "int" ?
    I can't use an offset like you used in your sample code (i.e. 0x680) because I have to read the unicode from a file and they are not continuous from 4E00 until the end, there are some gaps.

Similar Threads

  1. Form Based Authentication fails with unicode characters
    By davesquire in forum Java Servlet
    Replies: 1
    Last Post: 09-25-2009, 06:00 PM
  2. writing and reading unicode characters from a file
    By ranoosh in forum Advanced Java
    Replies: 4
    Last Post: 09-28-2008, 04:34 AM
  3. Displaying Japanese characters in JSP page
    By jmanjunath in forum Advanced Java
    Replies: 1
    Last Post: 09-25-2008, 11:19 AM
  4. Displaying COPYRIGHT and REGISTERED characters
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 03-01-2008, 09:52 PM
  5. Displaying characters in many ways.
    By TampaTechGuy in forum New To Java
    Replies: 7
    Last Post: 01-02-2008, 09:16 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts