• Post Reply Bookmark Topic Watch Topic
  • New Topic

JTextPane ignoring new lines when text is loaded from servlet

 
Martin Syska
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, I have a problem with new line character when passing a string I get from a servlet to JTextPane.

There's a text file on server that contains:



I have a servlet that returns the content of this file as a string. When I test servlet in a web browser it prints text in new lines correctly. But when I load it in my swing application and pass it to my textpane it looses new lines character and I get:


Of course I tried to load text directly from a file to a textpane and it loads ok (with new lines).

Servlet Code:

Reading text from file (simplified):


sending text:


My app code:


Please help me on this.
Martin
 
Rob Spoor
Sheriff
Posts: 20820
68
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What's the content type of your JTextPane? If it's text/html then that ignores whitespace mostly. Try setting it to text/plain.
 
Martin Syska
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Prime wrote:Try setting it to text/plain.


I tried it but it didn't help.
Also when I open file that I saved on disk using this servlet (It works both ways) in notepad I don't have new lines, but when I open the same file in WordPad everything is ok.

I pass data to servlet this way (utf-8 encoding):
 
Rob Spoor
Sheriff
Posts: 20820
68
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Martin Syska wrote:Also when I open file that I saved on disk using this servlet (It works both ways) in notepad I don't have new lines, but when I open the same file in WordPad everything is ok.

That's because notepad is garbage; it can only handle \r\n as line breaks; \n alone will not work. Just about every other text editor is better.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!