Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
360 views
in Technique[技术] by (71.8m points)

java - Getting raw text from JTextPane

In my application, I use a JTextPane to display some log information. As I want to hightlight some specific lines in this text (for example the error messages), I set the contentType as "text/html". This way, I can format my text.

Now, I create a JButton that copies the content of this JTextPane into the clipboard. That part is easy, but my problem is that when I call myTextPane.getText(), I get the HTML code, such as :

<html>
  <head>

  </head>
  <body>
    blabla<br>
    <font color="#FFCC66"><b>foobar</b></font><br>
    blabla
  </body>
</html>

instead of getting only the raw content:

blabla
foobar
blabla

Is there a way to get only the content of my JTextPane in plain text? Or do I need to transform the HTML into raw text by myself?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

No need to use the ParserCallback. Just use:

textPane.getDocument().getText(0, textPane.getDocument().getLength()) );

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...