Wednesday, April 11, 2012

Some of the Greek Characters in Java.AWT.Label are displayed as a box

When I read a greek string from a txt file encoded in UTF-8 format and try to display it as a Frame(Java.awt.frame) header it works fine, but when I set the same string as the text of a Label(Java.awt.Label) or Button(java.awt.Button), some of the characters (eg: '\u1F09' , especially the characters with with a quote on their head) are displayed as a box.



I have tried various fonts for the Label which support Greek characters like "DejaVu Sans", "Arial Unicode MS", "Cardo", etc but I wasnt able to get rid of the box character.



When I change the awt code to swing code (i.e., change Label to JLabel) the same code displays the correct Greek string. But it will be very difficult for me to replace every awt component with a swing component as it is a very large codebase.



Any help to solve this problem would be really appreciated. Thanks in advance !



Here is the code.. and I am running it on windows for now, but I need it to work on windows and MAC.



BufferedReader rd = null;
try {
rd = new BufferedReader(new InputStreamReader(new FileInputStream("D:/file.txt"), "UTF8"));
} catch (UnsupportedEncodingException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
String s = null;
while ((s = rd.readLine()) != null) {

Label label = new Label();
Font f = new Font("DejaVu Sans Mono", Font.PLAIN, 15);
label.setFont(f);
label.setText(s);
mainPanel.add(label);

}
}


the one thing that makes it even more confusing is that.. when I run the same applet (which has AWT objects) on Eclipse with (widows->preferences->General->Workspace->Text file encoding as UTF-8) .. i am able to see the proper text. So, I was wondering if there is any way by which we can do this programatically.





1 comment:

  1. try this:


    http://java.sun.com/javase/technologies/core/basic/intl/faq.jsp#font-awt-swing

    ReplyDelete