The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> Java Help
|
SetFont doesn't work for Chinese in JTextPane on Fedora
Discuss SetFont doesn't work for Chinese in JTextPane on Fedora in the Java Help forum on Dev Shed. SetFont doesn't work for Chinese in JTextPane on Fedora Java Help forum discussing all Java platforms - J2ME, J2SE and J2EE - as well as relevant standards, APIs and frameworks such as Swing, Servlets, JSPs, Applets, Struts, Spring, Hibernate, ANT, EJB, and other Java-related topics.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

December 13th, 2012, 02:31 AM
|
|
Contributing User
|
|
Join Date: Oct 2004
Posts: 244

Time spent in forums: 1 Day 17 h 29 m 59 sec
Reputation Power: 9
|
|
|
SetFont doesn't work for Chinese in JTextPane on Fedora
Hello, in the following code, there is a Chinese word in the html tag. If I set content type as "text/html", then it can't display Chinese correctly; for plain text, it's fine. I am working on Fedora linux. Can't figure out what's the problem. Thank you for any comments.
Code:
import java.awt.Font;
import java.awt.GraphicsEnvironment;
import java.util.Locale;
import javax.swing.*;
public class MyFont extends JPanel{
public MyFont() {
String text = "<html lang=\"zh\">\n" +
"Color and font test:\n" +
"<ul>\n" +
"<li><font color=red>中国</font>\n" +
"<li><font color=blue>blue</font>\n" +
"<li><font color=green>green</font>\n" +
"<li><font size=-2>small</font>\n" +
"<li><font size=+2>large</font>\n" +
"<li><i>italic</i>\n" +
"<li><b>bold</b>\n" +
"</ul>\n";
JTextPane htmlTextArea = new JTextPane();
htmlTextArea.setContentType("text/html");
htmlTextArea.setFont(getChineseFont());
htmlTextArea.setText(text);
JPanel panel = new JPanel();
panel.add(htmlTextArea);
add(panel);
}
private Font getChineseFont(){
Font[] allfonts = GraphicsEnvironment.getLocalGraphicsEnvironment().getAllFonts();
String chinesesample = "中文";
for (int j = 0; j < allfonts.length; j++) {
if (allfonts[j].canDisplayUpTo(chinesesample) == -1) {
System.out.println(allfonts[j].getName());
return new Font(allfonts[j].getFontName(), Font.PLAIN, 16);
}
}
return null;
}
public static void main(String[] args) {
JFrame frame = new JFrame("HtmlDemo");
frame.add(new MyFont());
frame.pack();
frame.setVisible(true);
}
}
|

December 13th, 2012, 03:53 AM
|
 |
Banned ;)
|
|
Join Date: Nov 2001
Location: Woodland Hills, Los Angeles County, California, USA
|
|
Where you are setting the content type for the htmlTextArea, try setting the type like this instead:
Code:
htmlTextArea.setContentType("text/html;charset=big5");
See if that works for you.
__________________
Up the Irons
What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home.
"Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest
Down with Sharon Osbourne
|

December 13th, 2012, 09:47 PM
|
|
Contributing User
|
|
Join Date: Oct 2004
Posts: 244

Time spent in forums: 1 Day 17 h 29 m 59 sec
Reputation Power: 9
|
|
Quote: | Originally Posted by Scorpions4ever Where you are setting the content type for the htmlTextArea, try setting the type like this instead:
Code:
htmlTextArea.setContentType("text/html;charset=big5");
See if that works for you. |
This doesn't work either.
|

December 14th, 2012, 06:28 AM
|
 |
Daniel Schildsky
|
|
Join Date: Mar 2004
Location: KL, Malaysia.
|
|
|
Unicode escape sequence
Try convert the Chinese characters to unicode entities:
Java Code:
Original
- Java Code |
|
|
|
import java.awt.Font; import java.awt.GraphicsEnvironment; import java.util.Locale; import javax.swing.*; public class MyFont extends JPanel{ public MyFont() { String text = "<html lang=\"zh\">\n" + "Color and font test:\n" + "<ul>\n" + "<li><font color=red>"+"&#"+"20013;"+"&#"+"22269;"+"</font>\n" + "<li><font color=blue>blue</font>\n" + "<li><font color=green>green</font>\n" + "<li><font size=-2>small</font>\n" + "<li><font size=+2>large</font>\n" + "<li><i>italic</i>\n" + "<li><b>bold</b>\n" + "</ul>\n"; htmlTextArea.setContentType("text/html"); htmlTextArea.setFont(getChineseFont()); htmlTextArea.setText(text); panel.add(htmlTextArea); add(panel); } private Font getChineseFont (){ String chinesesample = "&#"+ "20013;"+ "&#"+ "25991;"; for (int j = 0; j < allfonts.length; j++) { if (allfonts[j].canDisplayUpTo(chinesesample) == -1) { System. out. println(allfonts [j ]. getName()); return new Font(allfonts [j ]. getFontName(), Font. PLAIN, 16); } } return null; } public static void main (String[] args ) { frame.add(new MyFont()); frame.pack(); frame.setVisible(true); } }
Note:
I have to break up the unicode entities as a series of string concatenation due to the browser's conversion of unicode entities back to Chinese characters when the post was displayed.
__________________
When the programming world turns decent, the real world will turn upside down.
Last edited by tvc3mye : December 14th, 2012 at 06:35 AM.
Reason: make the unicode entities display as is.
|

December 15th, 2012, 02:53 PM
|
|
Contributing User
|
|
Join Date: Oct 2004
Posts: 244

Time spent in forums: 1 Day 17 h 29 m 59 sec
Reputation Power: 9
|
|
Quote: | Originally Posted by tvc3mye Try convert the Chinese characters to unicode entities:
Java Code:
Original
- Java Code |
|
|
|
import java.awt.Font; import java.awt.GraphicsEnvironment; import java.util.Locale; import javax.swing.*; public class MyFont extends JPanel{ public MyFont() { String text = "<html lang=\"zh\">\n" + "Color and font test:\n" + "<ul>\n" + "<li><font color=red>"+"&#"+"20013;"+"&#"+"22269;"+"</font>\n" + "<li><font color=blue>blue</font>\n" + "<li><font color=green>green</font>\n" + "<li><font size=-2>small</font>\n" + "<li><font size=+2>large</font>\n" + "<li><i>italic</i>\n" + "<li><b>bold</b>\n" + "</ul>\n"; htmlTextArea.setContentType("text/html"); htmlTextArea.setFont(getChineseFont()); htmlTextArea.setText(text); panel.add(htmlTextArea); add(panel); } private Font getChineseFont (){ String chinesesample = "&#"+ "20013;"+ "&#"+ "25991;"; for (int j = 0; j < allfonts.length; j++) { if (allfonts[j].canDisplayUpTo(chinesesample) == -1) { System. out. println(allfonts [j ]. getName()); return new Font(allfonts [j ]. getFontName(), Font. PLAIN, 16); } } return null; } public static void main (String[] args ) { frame.add(new MyFont()); frame.pack(); frame.setVisible(true); } }
Note:
I have to break up the unicode entities as a series of string concatenation due to the browser's conversion of unicode entities back to Chinese characters when the post was displayed. |
Does that work for you? I got the same problem on Fedora.
|

December 17th, 2012, 12:44 AM
|
 |
Daniel Schildsky
|
|
Join Date: Mar 2004
Location: KL, Malaysia.
|
|
|
Font problem?
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|