• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

"Unparsed" unicode String in JTextPane

Benjamin Weaver
Ranch Hand
Posts: 161
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When I set the text of a JTextPane with unicode characters in the init() method of my applet, Greek characters display without any problems.
For example, the following string will be "parsed" into Greek characters:


However, if I seek to CONVERT Romanized "Betacode" (e.g. "A/GG\k*T") into into unicode, using a "convert" Button while the Applet is up and running, the unicode is not "parsed."

The Button event handling and invocation of the translation into unicode is perfect, yielding the string I listed above. But the string appears in the JTextPane just as is, without being "parsed" in the Greek characters.

I am calling repaint() subsequent to the translation.

Why is the unicode not being "parsed"?

ps. I posted this under the Applet categorization, but later realized it might be best suited to the Swing/GUI category
Benjamin Weaver
Ranch Hand
Posts: 161
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bill (et al.),

Finally figured out a solution, even if it's a bit ungainly: to use the javac.Main class within my program to input the unicode string, compile it in a sub-program, and execute the convert() method of this sub-program which returns the "parsed" UTF-8 encoded version of the String.

There must be escape character processors out there that would do the job much more efficiently, but I haven't been able to locate one.

The embedded program looks like:

System.out.println("unicode is: " + unicode);

PrintWriter out = new PrintWriter(new FileOutputStream(file));
out.println(" * Source created on " + new Date());
out.println(" */");
out.println("public class " + classname + " {");

//a method in the class

out.println(" public static String convert() { ");
out.println(" String s = \"" + unicode + "\";" );
out.println(" return s; ");
out.println(" } ");


out.println(" ");

// Flush and close the stream


// Compile

String[] args = new String[] {
"-d", System.getProperty("user.dir"),
int status = javac.compile(args);

// Run

switch (status) {
case 0: // OK
// Make the class file temporary as well

new File(file.getParent(), classname + ".class").deleteOnExit();

try {
// Try to access the class and run its main method

Class clazz = Class.forName(classname);

Method convert = clazz.getMethod("convert", null);
parsedUnicode = (String) convert.invoke(null, null);
//String myUnicode = "\u1f82\u1f26\n\u1f82";
} catch (InvocationTargetException ex) {
// Exception in the main method that we just tried to run

showMsg("Exception in main: " + ex.getTargetException());
} catch (Exception ex) {
case 1: showMsg("Compile status: ERROR"); break;
case 2: showMsg("Compile status: CMDERR"); break;
case 3: showMsg("Compile status: SYSERR"); break;
case 4: showMsg("Compile status: ABNORMAL"); break;
showMsg("Compile status: Unknown exit status");
return parsedUnicode;
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic