Problem with Non English Chars

OS : Mac OS

Java : 1.5.0_07

Hi,

i have an Swing application that reads data from a database and shows them in a swing GUI. The text returned by the database is in Arabic and saved in a TextField object.

But once printed, the arabic chars are screwed up.or actually they r not arabic chars at all!!

For debugging i also write the result of the query in the console and in a log4j log file.

There, it is printed in the right form.

here the code:

System.out.println("D3"+java.nio.charset.Charset.defaultCharset().name());

System.out.println("singular "+dit.getData().getSingular());

log4j,debug("singular "+dit.getData().getSingular());

Font font = Font.decode("Geeza Pro");

textl.setFont(font);

textl.setText(dit.getData().getSingular());

The output in the console is (and log4j) :

D3MacRoman

singular صوف

The output in the Swing Textfield is

禑酂?br>

If i configure log4j to use UTF8 ,then even into log4j log file the same screwed

chars are written.

Looks like i've to tell Swing to use MacRoman, which is the default of the OS and

the used by the console&log4j. but i don't know how to.

Any clue?

Thanks,

Chris.

[1295 byte] By [ChrisVerzaa] at [2007-11-27 9:32:17]
# 1

convert your strings to unicode:

example 1

import java.awt.*;

import java.awt.event.*;

public class ApplicationFrame

extends Frame {

public ApplicationFrame() { this("ApplicationFrame v1.0"); }

public ApplicationFrame(String title) {

super(title);

createUI();

}

protected void createUI() {

setSize(500, 400);

center();

addWindowListener(new WindowAdapter() {

public void windowClosing(WindowEvent e) {

dispose();

System.exit(0);

}

});

}

public void center() {

Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();

Dimension frameSize = getSize();

int x = (screenSize.width - frameSize.width) / 2;

int y = (screenSize.height - frameSize.height) / 2;

setLocation(x, y);

}

}

import java.awt.*;

public class BidirectionalText {

public static void main(String[] args) {

Frame f = new ApplicationFrame("BidirectionalText v1.0") {

public void paint(Graphics g) {

Graphics2D g2 = (Graphics2D)g;

g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,

RenderingHints.VALUE_ANTIALIAS_ON);

Font font = new Font("Lucida Sans Regular", Font.PLAIN, 32);

g2.setFont(font);

g2.drawString("Please \u062e\u0644\u0639 slowly.", 40, 80);

}

};

f.setVisible(true);

}

}

example2

/*

Java Internationalization

By Andy Deitsch, David Czarnecki

ISBN: 0-596-00019-7

O'Reilly

*/

import java.awt.event.*;

import java.awt.*;

import java.text.*;

import javax.swing.*;

public class ArabicDigits extends JPanel {

static JFrame frame;

public ArabicDigits() {

NumberFormat nf = NumberFormat.getInstance();

if (nf instanceof DecimalFormat) {

DecimalFormat df = (DecimalFormat)nf;

DecimalFormatSymbols dfs = df.getDecimalFormatSymbols();

// set the beginning of the range to Arabic digits

dfs.setZeroDigit('\u0660');

df.setDecimalFormatSymbols(dfs);

}

// create a label with the formatted number

JLabel label = new JLabel(nf.format(1234567.89));

// set the font with a large enough size so we can easily

// read the numbers

label.setFont(new Font("Lucida Sans", Font.PLAIN, 22));

add(label);

}

public static void main(String [] argv) {

ArabicDigits panel = new ArabicDigits();

frame = new JFrame("Arabic Digits");

frame.addWindowListener(new WindowAdapter() {

public void windowClosing(WindowEvent e) {System.exit(0);}});

frame.getContentPane().add("Center", panel);

frame.pack();

frame.setVisible(true);

}

}

To avoid having to type all the \u... notation manually, use the native2ascii tool (included with the SDK).

http://java.sun.com/developer/technicalArticles/Intl/HTTPCharset/

java_2006a at 2007-7-12 22:49:22 > top of Java-index,Desktop,Core GUI APIs...
# 2
The arabic text is not hardcoded into variables but is read from a database though.If i do hardcode arabic text into a variable, it is then displayed fine in the textField !!
ChrisVerzaa at 2007-7-12 22:49:22 > top of Java-index,Desktop,Core GUI APIs...
# 3
any clue ?Why the same String is showed fine in the console and not in the GUI ?
ChrisVerzaa at 2007-7-12 22:49:22 > top of Java-index,Desktop,Core GUI APIs...