Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

JTextPane and new lines  RSS feed

 
Shane Roylance
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a JTextPane that is populated by output text from a program (report). The text being inserted in the JTextPane has new line ('\n') characters for line breaks. There is a little blurb in the javadoc comments of JTextPane (well it points you to the DefaultEditorKit), that says:
"There are two properties which deal with newlines. The system property, line.separator, is defined to be platform-dependent, either "\n", "\r", or "\r\n". There is also a property defined in DefaultEditorKit, called EndOfLineStringProperty, which is defined automatically when a document is loaded, to be the first occurrence of any of the newline characters. When a document is loaded, EndOfLineStringProperty is set appropriately, and when the document is written back out, the EndOfLineStringProperty is used. But while the document is in memory, the "\n" character is used to define a newline, regardless of how the newline is defined when the document is on disk. Therefore, for searching purposes, "\n" should always be used. When a new document is created, and the EndOfLineStringProperty has not been defined, it will use the System property when writing out the document."
The bottom line is, I am getting carriage returns in my text, and I don't want them. Can anyone show me how to manually change this so JTextPane does not insert carriage returns? (I am running this on a Windows platform)
 
Brian Cole
Author
Ranch Hand
Posts: 935
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Shane Roylance:
The bottom line is, I am getting carriage returns in my text, and I don't want them. Can anyone show me how to manually change this so JTextPane does not insert carriage returns?


JTextPane.read() does transform the carriage returns to
newlines, so there are no carriage returns in the pane's
Document. But pane.getText() invokes pane.write(), which
delegates to the EditorKit's write() method, which is
designed by default to put the carriage returns back again.

One way around this is to call
pane.getDocument().putProperty(DefaultEditorKit.EndOfLineStringProperty, "\n");
between the calls to read() and getText()/write().
[ August 04, 2006: Message edited by: Brian Cole ]
 
Brian Cole
Author
Ranch Hand
Posts: 935
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The other way is instead of calling pane.getText() to
call one of the pane.getDocument().getText(...) methods.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!