Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 515717
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T07:39:24+00:00 2026-05-13T07:39:24+00:00

In my application, I use a JTextPane to display some log information. As I

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T07:39:24+00:00Added an answer on May 13, 2026 at 7:39 am

    Based on the accepted answer to: Removing HTML from a Java String

    MyHtml2Text parser = new MyHtml2Text();
    try {
        parser.parse(new StringReader(myTextPane.getText()));
    } catch (IOException ee) {
      //handle exception
    }
    System.out.println(parser.getText());
    

    Slightly modified version of the Html2Text class found on the answer I linked to

    import java.io.IOException;
    import javax.swing.text.html.*;
    import javax.swing.text.html.parser.*;
    
    public class MyHtml2Text extends HTMLEditorKit.ParserCallback {
        StringBuffer s;
        public MyHtml2Text() {}
        public void parse(Reader in) throws IOException {
            s = new StringBuffer();
            ParserDelegator delegator = new ParserDelegator();
            delegator.parse(in, this, Boolean.TRUE);
        }
        public void handleText(char[] text, int pos) {
            s.append(text);
            s.append("\n");
        }
        public String getText() {
            return s.toString();
        }
    }
    

    If you need a more fine-grained handling consider implementing more of the interface defined by HTMLEditorKit.ParserCallback

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am wanting to expose some data service endpoints for internal application use and
In my rails application I use some external web services: twitter, google picasa, flickr
My application makes use of a SQLite database to store the user's inputs. The
I'm converting an application to use Java 1.5 and have found the following method:
I need my .net application to use the .html extension instead of .aspx I'm
I've just switched an application to use ar_mailer and when I run ar_sendmail (after
I need my ASP.NET web application to use silverlight controls in my web page.
How do I make my application always use English when displaying win32/.net exceptions messages?
Have Log4Net configured in our application to use a date stamped name and a
I create new ASP.NET web application that use SMTP to send message. The problem

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.