Results 1 to 20 of 21
- 07-08-2011, 01:27 PM #1
Member
- Join Date
- Jun 2011
- Posts
- 17
- Rep Power
- 0
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 :)
- 07-08-2011, 01:52 PM #2
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.
- 07-10-2011, 12:50 PM #3
Member
- Join Date
- Jun 2011
- Posts
- 17
- Rep Power
- 0
ok, but i am still very much stuck on this.
can anyone show me a working example?
- 07-10-2011, 12:58 PM #4
Member
- Join Date
- Jun 2011
- Posts
- 17
- Rep Power
- 0
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 :(
- 07-10-2011, 01:39 PM #5
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
The little snippet you showed passes "UTF-8" in as an argument to the InputStreamReader constructor.
Does
This may work(I'm not sure), but it's worth a shot.Java Code:in = new BufferedReader(new InputStreamReader(sock.getInoutStream(), "UTF-8"));
- 07-10-2011, 01:42 PM #6
Member
- Join Date
- Jun 2011
- Posts
- 17
- Rep Power
- 0
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
- 07-10-2011, 01:52 PM #7
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
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
- 07-10-2011, 03:24 PM #8
Member
- Join Date
- Jun 2011
- Posts
- 17
- Rep Power
- 0
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.
- 07-10-2011, 03:35 PM #9
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,373
- Blog Entries
- 7
- Rep Power
- 17
When people rob a bank they get a penalty; when banks rob people they get a bonus.
- 07-10-2011, 04:19 PM #10
You can use this snippet to identify the Fonts that has glyphs for the characters of interest:
dbJava 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()); } } }
- 07-10-2011, 04:24 PM #11
Member
- Join Date
- Jun 2011
- Posts
- 17
- Rep Power
- 0
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.
- 07-10-2011, 04:28 PM #12
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,373
- Blog Entries
- 7
- Rep Power
- 17
Strange, when I test that nice little method with a String "foo", all I get is:
I'm sure Courier New (to name one) can display foo just as well ...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
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 07-10-2011, 04:29 PM #13
Member
- Join Date
- Jun 2011
- Posts
- 17
- Rep Power
- 0
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 workingLast edited by mattxo; 07-10-2011 at 04:34 PM.
- 07-10-2011, 04:29 PM #14
Are there other classes and methods involved that can convert the bytes to a different font/charset?send it via a socket
- 07-10-2011, 05:38 PM #15
- 07-10-2011, 07:29 PM #16
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,373
- Blog Entries
- 7
- Rep Power
- 17
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!*When people rob a bank they get a penalty; when banks rob people they get a bonus.
- 07-11-2011, 12:53 PM #17
Member
- Join Date
- Jun 2011
- Posts
- 17
- Rep Power
- 0
- 07-11-2011, 01:22 PM #18
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
- 07-11-2011, 01:40 PM #19
Member
- Join Date
- Jun 2011
- Posts
- 17
- Rep Power
- 0
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.
- 07-11-2011, 04:07 PM #20
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
Similar Threads
-
Languages
By tim in forum Forum LobbyReplies: 0Last Post: 07-17-2010, 07:00 PM -
How to store other languages data in DB
By Deepa in forum New To JavaReplies: 3Last Post: 12-23-2009, 03:52 PM -
Languages for J2EE ??
By mgm2010 in forum New To JavaReplies: 1Last Post: 05-27-2009, 06:49 AM -
How to support multiple languages?
By bhupal4all in forum New To JavaReplies: 2Last Post: 09-05-2008, 09:58 AM -
Question about OO in other languages
By jurka in forum New To JavaReplies: 1Last Post: 07-17-2008, 10:01 PM


LinkBack URL
About LinkBacks
Reply With Quote
.gif)

Bookmarks