Java Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesJava Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old December 13th, 2012, 02:31 AM
marlonmin marlonmin is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2004
Posts: 244 marlonmin User rank is Private First Class (20 - 50 Reputation Level)marlonmin User rank is Private First Class (20 - 50 Reputation Level) 
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);
    }
}

Reply With Quote
  #2  
Old December 13th, 2012, 03:53 AM
Scorpions4ever's Avatar
Scorpions4ever Scorpions4ever is offline
Banned ;)
Dev Shed God 9th Plane (9000 - 9499 posts)
 
Join Date: Nov 2001
Location: Woodland Hills, Los Angeles County, California, USA
Posts: 9,406 Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level) 
Time spent in forums: 2 Months 9 h 47 m 28 sec
Reputation Power: 4080
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

Reply With Quote
  #3  
Old December 13th, 2012, 09:47 PM
marlonmin marlonmin is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2004
Posts: 244 marlonmin User rank is Private First Class (20 - 50 Reputation Level)marlonmin User rank is Private First Class (20 - 50 Reputation Level) 
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.

Reply With Quote
  #4  
Old December 14th, 2012, 06:28 AM
tvc3mye's Avatar
tvc3mye tvc3mye is offline
Daniel Schildsky
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Mar 2004
Location: KL, Malaysia.
Posts: 1,534 tvc3mye User rank is General 10th Grade (Above 100000 Reputation Level)tvc3mye User rank is General 10th Grade (Above 100000 Reputation Level)tvc3mye User rank is General 10th Grade (Above 100000 Reputation Level)tvc3mye User rank is General 10th Grade (Above 100000 Reputation Level)tvc3mye User rank is General 10th Grade (Above 100000 Reputation Level)tvc3mye User rank is General 10th Grade (Above 100000 Reputation Level)tvc3mye User rank is General 10th Grade (Above 100000 Reputation Level)tvc3mye User rank is General 10th Grade (Above 100000 Reputation Level)tvc3mye User rank is General 10th Grade (Above 100000 Reputation Level)tvc3mye User rank is General 10th Grade (Above 100000 Reputation Level)tvc3mye User rank is General 10th Grade (Above 100000 Reputation Level)tvc3mye User rank is General 10th Grade (Above 100000 Reputation Level)tvc3mye User rank is General 10th Grade (Above 100000 Reputation Level)tvc3mye User rank is General 10th Grade (Above 100000 Reputation Level)tvc3mye User rank is General 10th Grade (Above 100000 Reputation Level)tvc3mye User rank is General 10th Grade (Above 100000 Reputation Level) 
Time spent in forums: 2 Weeks 4 Days 2 h 27 m 57 sec
Reputation Power: 1620
Send a message via MSN to tvc3mye Send a message via Yahoo to tvc3mye Send a message via Google Talk to tvc3mye Send a message via Skype to tvc3mye
Facebook
Unicode escape sequence

Try convert the Chinese characters to unicode entities:

Java Code:
Original - Java Code
  1.  
  2. import java.awt.Font;
  3. import java.awt.GraphicsEnvironment;
  4. import java.util.Locale;
  5.  
  6. import javax.swing.*;
  7.  
  8.  public class MyFont extends JPanel{
  9.  
  10.     public MyFont() {
  11.        
  12.         String text = "<html lang=\"zh\">\n" +
  13.                 "Color and font test:\n" +
  14.                 "<ul>\n" +
  15.                 "<li><font color=red>"+"&#"+"20013;"+"&#"+"22269;"+"</font>\n" +
  16.                 "<li><font color=blue>blue</font>\n" +
  17.                 "<li><font color=green>green</font>\n" +
  18.                 "<li><font size=-2>small</font>\n" +
  19.                 "<li><font size=+2>large</font>\n" +
  20.                 "<li><i>italic</i>\n" +
  21.                 "<li><b>bold</b>\n" +
  22.                 "</ul>\n";
  23.  
  24.         JTextPane htmlTextArea = new JTextPane();
  25.         htmlTextArea.setContentType("text/html");
  26.         htmlTextArea.setFont(getChineseFont());
  27.         htmlTextArea.setText(text);
  28.         JPanel panel = new JPanel();
  29.         panel.add(htmlTextArea);
  30.         add(panel);
  31.        
  32.     }
  33.  
  34.     private Font getChineseFont(){
  35.        
  36.         Font[] allfonts = GraphicsEnvironment.getLocalGraphicsEnvironment().getAllFonts();
  37.         String chinesesample = "&#"+"20013;"+ "&#"+ "25991;";
  38.         for (int j = 0; j < allfonts.length; j++) {
  39.             if (allfonts[j].canDisplayUpTo(chinesesample) == -1) {
  40.                 System.out.println(allfonts[j].getName());
  41.                 return new Font(allfonts[j].getFontName(), Font.PLAIN, 16);
  42.             }         
  43.         }
  44.         return null;
  45.    }
  46.     public static void main(String[] args) {
  47.         JFrame frame = new JFrame("HtmlDemo");
  48.         frame.add(new MyFont());
  49.         frame.pack();
  50.         frame.setVisible(true);
  51.     }
  52. }
  53.  


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.

Reply With Quote
  #5  
Old December 15th, 2012, 02:53 PM
marlonmin marlonmin is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2004
Posts: 244 marlonmin User rank is Private First Class (20 - 50 Reputation Level)marlonmin User rank is Private First Class (20 - 50 Reputation Level) 
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
  1.  
  2. import java.awt.Font;
  3. import java.awt.GraphicsEnvironment;
  4. import java.util.Locale;
  5.  
  6. import javax.swing.*;
  7.  
  8.  public class MyFont extends JPanel{
  9.  
  10.     public MyFont() {
  11.        
  12.         String text = "<html lang=\"zh\">\n" +
  13.                 "Color and font test:\n" +
  14.                 "<ul>\n" +
  15.                 "<li><font color=red>"+"&#"+"20013;"+"&#"+"22269;"+"</font>\n" +
  16.                 "<li><font color=blue>blue</font>\n" +
  17.                 "<li><font color=green>green</font>\n" +
  18.                 "<li><font size=-2>small</font>\n" +
  19.                 "<li><font size=+2>large</font>\n" +
  20.                 "<li><i>italic</i>\n" +
  21.                 "<li><b>bold</b>\n" +
  22.                 "</ul>\n";
  23.  
  24.         JTextPane htmlTextArea = new JTextPane();
  25.         htmlTextArea.setContentType("text/html");
  26.         htmlTextArea.setFont(getChineseFont());
  27.         htmlTextArea.setText(text);
  28.         JPanel panel = new JPanel();
  29.         panel.add(htmlTextArea);
  30.         add(panel);
  31.        
  32.     }
  33.  
  34.     private Font getChineseFont(){
  35.        
  36.         Font[] allfonts = GraphicsEnvironment.getLocalGraphicsEnvironment().getAllFonts();
  37.         String chinesesample = "&#"+"20013;"+ "&#"+ "25991;";
  38.         for (int j = 0; j < allfonts.length; j++) {
  39.             if (allfonts[j].canDisplayUpTo(chinesesample) == -1) {
  40.                 System.out.println(allfonts[j].getName());
  41.                 return new Font(allfonts[j].getFontName(), Font.PLAIN, 16);
  42.             }         
  43.         }
  44.         return null;
  45.    }
  46.     public static void main(String[] args) {
  47.         JFrame frame = new JFrame("HtmlDemo");
  48.         frame.add(new MyFont());
  49.         frame.pack();
  50.         frame.setVisible(true);
  51.     }
  52. }
  53.  


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.

Reply With Quote
  #6  
Old December 17th, 2012, 12:44 AM
tvc3mye's Avatar
tvc3mye tvc3mye is offline
Daniel Schildsky
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Mar 2004
Location: KL, Malaysia.
Posts: 1,534 tvc3mye User rank is General 10th Grade (Above 100000 Reputation Level)tvc3mye User rank is General 10th Grade (Above 100000 Reputation Level)tvc3mye User rank is General 10th Grade (Above 100000 Reputation Level)tvc3mye User rank is General 10th Grade (Above 100000 Reputation Level)tvc3mye User rank is General 10th Grade (Above 100000 Reputation Level)tvc3mye User rank is General 10th Grade (Above 100000 Reputation Level)tvc3mye User rank is General 10th Grade (Above 100000 Reputation Level)tvc3mye User rank is General 10th Grade (Above 100000 Reputation Level)tvc3mye User rank is General 10th Grade (Above 100000 Reputation Level)tvc3mye User rank is General 10th Grade (Above 100000 Reputation Level)tvc3mye User rank is General 10th Grade (Above 100000 Reputation Level)tvc3mye User rank is General 10th Grade (Above 100000 Reputation Level)tvc3mye User rank is General 10th Grade (Above 100000 Reputation Level)tvc3mye User rank is General 10th Grade (Above 100000 Reputation Level)tvc3mye User rank is General 10th Grade (Above 100000 Reputation Level)tvc3mye User rank is General 10th Grade (Above 100000 Reputation Level) 
Time spent in forums: 2 Weeks 4 Days 2 h 27 m 57 sec
Reputation Power: 1620
Send a message via MSN to tvc3mye Send a message via Yahoo to tvc3mye Send a message via Google Talk to tvc3mye Send a message via Skype to tvc3mye
Facebook
Font problem?

If this does not work on your OS, could it be that your Fedora does not have a font that supports Chinese character display.
The following post I found from StackOverflow forum may be helpful to you:

How to make java application display Chinese under Fedora core 5

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesJava Help > SetFont doesn't work for Chinese in JTextPane on Fedora

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap