Page 1 of 2 12 LastLast
Results 1 to 20 of 21
  1. #1
    mattxo is offline Member
    Join Date
    Jun 2011
    Posts
    17
    Rep Power
    0

    Default jTextPane and other languages! help please!

    Ok, So ive made a simple chat program but whenever a person speaks another language (specifically Thai) it doesn't come out in Thai it comes out in ASCII jibberish :P can someone tell me how I would make it display in Thai?

    Many thanks to whoever answers this question :)

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,394
    Rep Power
    25

    Default

    There are Charset options on some classes that would allow you to work with different character sets. The String class has a constructor that takes a Charset arg.

  3. #3
    mattxo is offline Member
    Join Date
    Jun 2011
    Posts
    17
    Rep Power
    0

    Default

    ok, but i am still very much stuck on this.

    can anyone show me a working example?

  4. #4
    mattxo is offline Member
    Join Date
    Jun 2011
    Posts
    17
    Rep Power
    0

    Default

    ok, so I have been doing a lot of research on this but all i can find are examples of this working with files, not straight from the socket. for example here is my code for my reader

    in = new BufferedReader(new InputStreamReader(sock.getInputStream()));

    but i have searched some examples which use character sets and here is an example

    InputStreamReader inputStreamReader =
    new InputStreamReader(new FileInputStream (file), "UTF-8" );

    How can i adjust my code to accept a character set, I dont believe BufferedReader allows it. please help :(

  5. #5
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    The little snippet you showed passes "UTF-8" in as an argument to the InputStreamReader constructor.

    Does
    Java Code:
    in = new BufferedReader(new InputStreamReader(sock.getInoutStream(), "UTF-8"));
    This may work(I'm not sure), but it's worth a shot.

  6. #6
    mattxo is offline Member
    Join Date
    Jun 2011
    Posts
    17
    Rep Power
    0

    Default

    ahh lol thanks man i swear i tried that in netbeans and it gave me error but your code works fine, thanks!! hopefully this will work but it will take me some time to test it :p

  7. #7
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    You are welcome, hopefully it works. The streams are all using the decorator pattern(to the best of my knowledge), and they forward to an underlying stream, which is passed an argument to the constructor. Read more here: Decorator pattern

  8. #8
    mattxo is offline Member
    Join Date
    Jun 2011
    Posts
    17
    Rep Power
    0

    Default

    Ok, im sure UTF-8 is the right format but it still is displaying as boxes in the JTextPane do i need to set the character set on the JTextPane to UTF-8?

    if so, how? ive been trying to no success.

  9. #9
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,516
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by mattxo View Post
    Ok, im sure UTF-8 is the right format but it still is displaying as boxes in the JTextPane do i need to set the character set on the JTextPane to UTF-8?

    if so, how? ive been trying to no success.
    The font used by the JTextPane has to be able to display the Thai character set; on a Windows box set the font to, e.g. Arial MS Unicode. The little boxes indicate that your font isn't able to display those characters.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  10. #10
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,234
    Rep Power
    19

    Default

    You can use this snippet to identify the Fonts that has glyphs for the characters of interest:
    Java Code:
    public static void testFonts(String toDisplay) {
       GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
       Font[] fonts = ge.getAllFonts();
       for (Font font : fonts) {
          if (font.canDisplayUpTo(toDisplay) != -1) {
             System.out.println(font.getName());
          }
       }
    }
    db

  11. #11
    mattxo is offline Member
    Join Date
    Jun 2011
    Posts
    17
    Rep Power
    0

    Default

    thanks for the help guys but im 100% sure the font is ok.

    I know this because when i type it in a JTextField and then add it directly to the jTextPane it shows correctly but when i send it via a socket it is unable to render the data correctly.

  12. #12
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,516
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by DarrylBurke View Post
    You can use this snippet to identify the Fonts that has glyphs for the characters of interest:
    Java Code:
    public static void testFonts(String toDisplay) {
       GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
       Font[] fonts = ge.getAllFonts();
       for (Font font : fonts) {
          if (font.canDisplayUpTo(toDisplay) != -1) {
             System.out.println(font.getName());
          }
       }
    }
    db
    Strange, when I test that nice little method with a String "foo", all I get is:

    Java Code:
    Bookshelf Symbol 7
    MS Reference 1
    MS Reference 2
    MS Reference Specialty
    MT Extra
    MV Boli
    Marlett
    Symbol
    Webdings
    Wingdings
    Wingdings 2
    Wingdings 3
    I'm sure Courier New (to name one) can display foo just as well ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  13. #13
    mattxo is offline Member
    Join Date
    Jun 2011
    Posts
    17
    Rep Power
    0

    Default

    here is my code for my bufferedreader/writer constructors

    in = new BufferedReader(new InputStreamReader(sock.getInputStream(), "UTF-8"));
    BufferedWriter out = new BufferedWriter(new OutputStreamWriter(sock.getOutputStream(), "UTF-8"));

    i cant understand why it wont work :\

    ive even tried to set the setContentType to "UTF-8" but still not working
    Last edited by mattxo; 07-10-2011 at 04:34 PM.

  14. #14
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,394
    Rep Power
    25

    Default

    send it via a socket
    Are there other classes and methods involved that can convert the bytes to a different font/charset?

  15. #15
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,234
    Rep Power
    19

    Default

    Quote Originally Posted by JosAH View Post
    Strange, when I test that nice little method with a String "foo", all I get is:

    Java Code:
    Bookshelf Symbol 7
    MS Reference 1
    MS Reference 2
    MS Reference Specialty
    MT Extra
    MV Boli
    Marlett
    Symbol
    Webdings
    Wingdings
    Wingdings 2
    Wingdings 3
    I'm sure Courier New (to name one) can display foo just as well ...
    Oops, sorry

    That should be
    Java Code:
    if (font.canDisplayUpTo(toDisplay) == -1) {
    db

  16. #16
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,516
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by DarrylBurke View Post
    Oops, sorry

    That should be
    Java Code:
    if (font.canDisplayUpTo(toDisplay) == -1) {
    db
    I'd like to call for a revolt! So my Wingdings font can't even display the String "foo" properly? What even half self respecting font can't display "foo"? It's a waste of disk space! It's a crippled font not worth the bytes it takes up on everybody's disk drive! Defenestrate the moron that came up with such a blasphemy of a font! Aaaarrggghhh!

    kind regards,

    Jos *duh!*
    cenosillicaphobia: the fear for an empty beer glass

  17. #17
    mattxo is offline Member
    Join Date
    Jun 2011
    Posts
    17
    Rep Power
    0

    Default

    Quote Originally Posted by Norm View Post
    Are there other classes and methods involved that can convert the bytes to a different font/charset?
    yes, the string is then sent to another class (the gui) and uses a method to add it to the chat window but I am still a beginner and do not know how to do what you say :(

  18. #18
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,394
    Rep Power
    25

    Default

    This is a hard project for me to work on as I don't have any sample data to work with. I understand that your problem is that characters (in Thai) typed into a text component are copied from there, sent via socket to another PC and displayed at the target in another text component. When they are displayed, they are not Thai characters.
    Can you post some Thai charcters here for testing? Maybe in Unicode format: \uxxxxx

  19. #19
    mattxo is offline Member
    Join Date
    Jun 2011
    Posts
    17
    Rep Power
    0

    Default

    yea sure i can give you some thai characters to work with, also, i found this about thai characters/unicode which you may be able to use. I dont know how to use Unicode in java.

    Thai €” Unicode Character Table

    here are some thai characters:

    ฟิแหกำดเ้ร่าสทืนยๆพหะีอไปัผ

    I've been programming quite a lot but this problem i just cant seem to get past and not many people seem to know either. I guess its quite a rare problem.

    Again, thanks for your help.

    edit: also, all information is sent via socket using UTF-8 so it should arrive in thai but let me know what ur results are.
    Last edited by mattxo; 07-11-2011 at 01:46 PM.

  20. #20
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,394
    Rep Power
    25

    Default

    Here's a start. There is still something that I don't understand about how the characters are displayed.
    Java Code:
    import java.io.*;
    import javax.swing.*;
    import java.awt.*;
    
    public class CharsetProblem2  extends JFrame {
       // The String to output and then input
       final String theChars = "Here are five Thai letters: *\u0E1F*\u0E34*\u0E41*\u0E2B*\u0E01*";  
       final String TheEnc = "Unicode";     // Set the charset name/encoding
    
    
       public static void main(String[] args) {
          new CharsetProblem2();
       } // end main()      
    
       JTextField tfIn = new JTextField();  // Display original string here
       JTextField tfOut = new JTextField(); // Display string after to bytes and back
    
       //-------------------------------
       public CharsetProblem2() {
          super("Work with Thai chars");
          JPanel ourPanel = new JPanel(new BorderLayout());
          ourPanel.add(tfIn, BorderLayout.NORTH);
          ourPanel.add(tfOut, BorderLayout.CENTER);
          setContentPane(ourPanel);
          setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          setSize(300, 100);
          setLocationRelativeTo(null);
          setVisible(true);
    
         try {
           tfIn.setText(theChars);   // Show the original String
    
           byte[] asBytes = theChars.getBytes(TheEnc);  
           System.out.println("for " + TheEnc + ": asBytes=" + java.util.Arrays.toString(asBytes));
           String newChars = new String(asBytes, TheEnc);
           System.out.println("comparing in:out = " + theChars.equals(newChars));
    //       System.out.println(theChars + "\n" + newChars);
         
           // Write the bytes into a byte buffer
           ByteArrayOutputStream baos = new ByteArrayOutputStream();  // instead of writing file
           BufferedWriter out = new BufferedWriter(new OutputStreamWriter(baos, TheEnc));
           out.write(theChars);
           out.close();
    
           byte[] theBytes = baos.toByteArray();
           // Data is now in a byte array 
           System.out.println("theBytes=" + java.util.Arrays.toString(theBytes));
    
           // Read it in and display in the second text field
           ByteArrayInputStream bais = new ByteArrayInputStream(theBytes);
           BufferedReader brdr = new BufferedReader(new InputStreamReader(bais , TheEnc));
           String readStr = brdr.readLine();
           tfOut.setText(readStr);  // show the resultant string
    
         } catch (IOException ex) {
            ex.printStackTrace();
         }
       } // end constructor 
    } // end class

Page 1 of 2 12 LastLast

Similar Threads

  1. Languages
    By tim in forum Forum Lobby
    Replies: 0
    Last Post: 07-17-2010, 07:00 PM
  2. How to store other languages data in DB
    By Deepa in forum New To Java
    Replies: 3
    Last Post: 12-23-2009, 03:52 PM
  3. Languages for J2EE ??
    By mgm2010 in forum New To Java
    Replies: 1
    Last Post: 05-27-2009, 06:49 AM
  4. How to support multiple languages?
    By bhupal4all in forum New To Java
    Replies: 2
    Last Post: 09-05-2008, 09:58 AM
  5. Question about OO in other languages
    By jurka in forum New To Java
    Replies: 1
    Last Post: 07-17-2008, 10:01 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
  •