HTMLEditorKit modify my text in JEditorPane HTML

948 Views Asked by At

I'm using a JTextPane to edit HTML and when I use getText() and setText() methods it changes my text.

e.g If I set this text with setter method.

<html> 
<head> 
</head> 
<body bgcolor="BLACK"> 
<font color = "WHITE">ESTO ES <br> 
UNA<br> 
PRUEBA<br> 
DE<br> 
SALTOS DE <br> 
LINEA<br> 
</font> 
</body> 
</html> 

And when I call getText() it returns

<html>
  <head>

  </head>
  <body bgcolor="BLACK">
    <font color="WHITE">ESTO ES<br>UNA<br>PRUEBA<br>DE<br>SALTOS DE<br>LINEA<br></font>
  </body>
</html>

It's important for me to keep the original format. Is it possible?

Thanks!

1

There are 1 best solutions below

1
On

The HTMLEditorKit parses the document and builds a dom tree, your original formatting gets lost anyway. Your getText() result is the correct output as HTML.

But maybe you could write a custom HtmlWriter to output in your own format.