How can I use a foreign language char in Swing

Jason Orendorff jason at jorendorff.com
Mon Mar 4 00:26:07 EST 2002


> I installed J2SDK 1.4.0 several days ago. I need to get some strings from
> database which stored in GB2312 encode method, and display it
> using Swing. I
> got correct result in Windows2000(Use China as default region and
> GB2312 as
> encode method.) While I¡¡run it on Windows(English Edition), all I can see
> is ?????.
>
> What should I do? I think ResourceBoundle can't help me. Is there
> any other
> method can help me?

(grin) It seems you're on the wrong newsgroup for this.

I'm not sure exactly what's happening.  Here are two possibilities.

  1.  Missing Fonts.  Seems unlikely.  But if this is the problem,
      it's easy to fix.  :)

  2.  Encodings.

      Perhaps this is happening:  the database innocently sends you
      some bytes, and Java must convert those bytes into a String.
      To do this, it must guess which encoding is appropriate.  Java
      makes the best guess it can: your operating system's default
      encoding.  Sometimes this is wrong, as you've discovered.

      In this case, you can usually fix the problem by telling
      Java which encoding to use.  It all depends on where you're
      getting the string.

      Try this:  when you run the program, specify
      -Dfile.encoding=gb2312 as an option to java.  This might or
      might not work.  I'm not sure all JVMs support the file.encoding
      property.  It doesn't seem to be very well documented.

      It would be better to get the raw bytes out of the database
      (in a byte[] array) and convert them to String by yourself, thus:

          String correctString = new String(bytes, "gb2312");

      Several Java standard library classes allow you to specify
      which encoding to use:  String, InputStreamReader, and
      OutputStreamWriter, for example.  You should specify the
      encoding whenever possible!  This helps avoid the kind of bug
      you're currently struggling with.

But perhaps both of my guesses are wrong.  In any case, good luck!

## Jason Orendorff    http://www.jorendorff.com/





More information about the Python-list mailing list