I am using a JTextPane object, let its name be jtp , so I can show parts of a few short lines in italics or bold . I have a String, name it otxt , which has a new line. Given that I am using Windows, this means \ r \ n and is created using
System.getProperty("line.separator")
and to dispel all doubts before they are fully formed, I converted the String array to a char array, went through the char array and wrote codes of all characters on the Eclipse console using implicit conversion by casting to int and yes, there is only one code 13 representing \ r and only one code 10 representing \ n. Now if i use
jtp.setText(otxt);
everything is going well - I am doing the conversion to a char array from
jtp.getText()
and print out the codes of all the characters, and I have only one code 13 (\ r) and only one code 10 (\ n), as I assume. But I need to show certain parts of otxt in italics or bold , so I have to use
jtp.insertString(0 , otxt, attributeSet )
And here's the big problem: this creates \ r \ r \ n where I should have \ r \ n. This is confirmed using the same procedure: 1) a string from the jtp.getText () array to char; 2) walk through the array; 3) print character codes; and I see code 13 (\ r) twice and code 10 (\ n) once. Help me fight this. Hope you find an interesting question.
source
share