Re: About JTextPane, Why "Not equal" ?
Tom Hawtin <usenet@tackline.plus.com> wrote or quoted in
Message-ID <460e25ea$0$8726$ed2619ec@ptn-nntp-reader02.plus.net>:
The code compares the raw text, so the attributes should come into it.
For comparing strings, what role do the above "attributes" play ?
Event if "p1.getDocument().insertString(0, s, p1.getInputAttributes())",
the result is "false" on JDK1.5.0_11 (WinXP).
The difference between JEditorPane.setText and Document.insertString
(other than that one replaces, etc) is that setText goes through the
EditorKit (which then calls insertString). So you can setText an entire
HTML document, if the JEditorPane is so configured.
For plain text, the PlainEditorKit attempts to cope with different new
line standard. It may not pass through the text unchanged. Mixing new
line standards, makes it go false for me.
String s = "\r\n123\n45\r\n6890";
^^
Let's suppose that String s = "\r\n12345\r\n6890" is received
from a remote server.
With the same code,
1) SadRed and you said "true".
2) Andrew Thompson and I said "false".
I think, it means that platform-independent code is not guaranteed
with "JTextPane". And also, it means that an app has bugs if both
"..setText" and "..insertString" are used in the app.
See this code.
<code>
import javax.swing.JTextPane;
import javax.swing.text.BadLocationException;
import javax.swing.text.DefaultEditorKit;
import javax.swing.text.Document;
public class Test_Swing {
public static void main(String[] args) throws BadLocationException {
String s = "\r\n0\r\n1";
JTextPane pane = new JTextPane();
pane.getDocument().insertString(0, s, pane.getInputAttributes());
String r = pane.getText();
for (int i = 0; i < r.length(); i++) {
System.out.println((int) r.charAt(i));
}
}
}
</code>
The above code prints this:
--
13
13
10
48
13
13
10
49
--
Note that 13(\r) was doubled.
Was "JTextPane" and "Document" designed properly ?