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

  • SEARCH
  • Home
  • 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'm using Visual Studio to create my installation package. My application use some bitmap
In my application I use a UIAlertView to display to the user a message
My application use a ListView which display different items in the cell. it must
I wrote an application that use MKMapView. This application use a timer to update
I am wiling to have some pages of my application use a wiki based
Hello I've just implemented a search option on my application use this on the
My application use Storage, but for testing I want to substitute it with mock
i currently build an application use spring as framework. and i want to test
I use the x64 version of Windows 7. My application use some COM servers
I am wanting to expose some data service endpoints for internal application use and

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.