JTextPane trouble

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • HxRLxY
    New Member
    • Sep 2008
    • 23

    JTextPane trouble

    I am an email client. I want to be able to send the emails in rich-text format, so the GUI uses a JTextPane as the Text Component in which the user's message is entered. The problem is that when I call textPane.getTex t() the returned string contains only HTML tags, no actual text. So my question is how do I get the HTML content from a JTextPane, including tags and text, to use in a JavaMail application? Here is the jist of what I have tried:

    Code:
    //a lot of GUI code
    
    JTextPane textPane = new JTextPane();
    textPane.setEditorKit( new HTMLEditorKit() );
    textPane.setDocument( new HTMLDocument() );
    
    //more GUI code
    
    String message = textPane.getText();
    
    //then code to send the email
    Having entered some arbitrarily formatted text and calling System.out.prin tln(message) the following output is produced. (The output of course varies with the random non-sense I enter into the JTextPane whilst testing, but it is always only tags)

    Code:
    <html>
      <head>
    
      </head>
      <body>
        <p style="margin-top: 0">
          <font face="Dialog" size="3"><Dialog>
          <u><Dialog>
          </u><b><Dialog>
          </b></font><b><font face="Dialog" size="7"><Dialog>
    </font></b>    </p>
      </body>
    </html>
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    What happens when you ask your HTMLEditorKit to write its entire content? Read the API docs for the details of its write() method. For the actual Writer you can use a StringWriter so you can get the content as a String later.

    kind regards,

    Jos

    Comment

    • HxRLxY
      New Member
      • Sep 2008
      • 23

      #3
      I have tried that, with the same result. I combed the API and thought this would work, but it does the same thing - prints only the tags.

      Code:
      HTMLEditorKit kit = (HTMLEditorKit)textPane.getEditorKit();
      HTMLDocument doc = (HTMLDocument)textPane.getDocument();
      StringWriter writer = new StringWriter();
      
      try{
      kit.write( writer, doc, 0, doc.getLength() );
      System.out.println( writer.toString() );
      }
      catch( IOException ie ){
      //code to handle exception 
      }
      Last edited by HxRLxY; Mar 8 '09, 04:27 PM. Reason: Clarification

      Comment

      • JosAH
        Recognized Expert MVP
        • Mar 2007
        • 11453

        #4
        I have a question for you: can you post a (preferably short) example that shows the behaviour you described, so that I can play with it? The API doesn't give me an answer and I find it a bit strange that there would be no way to get the original html back (I don't really care about comments in the text).

        kind regards,

        Jos

        Comment

        • HxRLxY
          New Member
          • Sep 2008
          • 23

          #5
          Well Jos, in the process of writing a small example for you I discovered the problem. For some reason setting more than one attribute at a time with SimpleAttribute Set causes the JTextPane to get confused and lose the text. So I modified the code to change only one attribute at a time, and voila. Thanks for the help

          -Harley

          Comment

          • JosAH
            Recognized Expert MVP
            • Mar 2007
            • 11453

            #6
            Originally posted by HxRLxY
            Well Jos, in the process of writing a small example for you I discovered the problem. For some reason setting more than one attribute at a time with SimpleAttribute Set causes the JTextPane to get confused and lose the text. So I modified the code to change only one attribute at a time, and voila. Thanks for the help
            You're welcome of course; what just happened to you is typical when you write an SSCCE, i.e. the mistake bubbles up to the surface and you're able to solve your problems yourself ;-)

            kind regards,

            Jos

            Comment

            Working...