Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-4331766

setCharacterAttributes not working properly on htmldocument

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Fixed
    • Icon: P3 P3
    • 1.4.0
    • 1.3.0, 1.4.0
    • client-libs
    • beta
    • x86
    • windows_nt



      Name: skT45625 Date: 04/19/2000


      java version "1.3.0rc3"
      Java(TM) 2 Runtime Environment, Standard Edition (build 1.3.0rc3-Z)
      Java HotSpot(TM) Client VM (build 1.3.0rc3-z, mixed mode)


      I'm trying to build a simple HTML editor. It's a JEditorPane, using an
      HTMLEditorKit, and an HTMLDocument. I then have bold, underline, and italic
      buttons, which are pressed to affect the selected text by using a
      MutableAttributeSet and setCharacterAttributes. This works fine, until the text
      is stored, and read back into the document at a later date. Say, for instance,
      I make the first character of my text bold, and then save it away somewhere.
      Looking at the HTML document, I can see that the b tags have been applied
      properly. When the document is read back in, the b tag is detected, and the
      first character is displayed as bold (as expected). Highlighting that
      character, and turning bold off causes it to go back to normal in the
      JeditorPane display, but the tag is left in the underlying document.
      I can't paste the whole code here (as it's very much a work in progress), but
      here's some sample code to illustrate the problem:

      import javax.swing.*;
      import java.awt.*;
      import javax.swing.text.html.HTMLEditorKit;
      import javax.swing.text.html.HTMLDocument;
      import javax.swing.text.html.*;
      import javax.swing.text.*;
      import java.io.Reader;





      public class HTMLtest extends JFrame
      {
          JEditorPane jEditorPane1 = new JEditorPane();
          BorderLayout borderLayout1 = new BorderLayout();
          HTMLEditorKit kit = new HTMLEditorKit();
          HTMLDocument doc = (HTMLDocument)(kit.createDefaultDocument());


          public HTMLtest()
          {
              try
              {
                  jbInit();
              }
              catch(Exception e)
              {
                  e.printStackTrace();
              }
          }

          private void jbInit() throws Exception
          {

              this.getContentPane().setLayout(borderLayout1);
              this.getContentPane().add(jEditorPane1, BorderLayout.CENTER);
              Reader in = new java.io.StringReader("12345");
              kit.read(in, doc, 0);
              in.close();
              jEditorPane1.setContentType("text/html");
              jEditorPane1.setEditorKit(kit);
              jEditorPane1.setDocument(doc);
          }


          public static void main(String args[])
          {
              HTMLtest htmltest = new HTMLtest();
              //print out the text before we modify it
              System.out.println(htmltest.jEditorPane1.getText());

              MutableAttributeSet attr = new SimpleAttributeSet();
              StyleConstants.setBold(attr, true);
              StyleConstants.setItalic(attr, true);
              htmltest.doc.setCharacterAttributes(1, 1,attr,false);

              //print out the text after we've made it bold and italic
              System.out.println(htmltest.jEditorPane1.getText());

              StyleConstants.setBold(attr, false);
              StyleConstants.setItalic(attr, false);
              htmltest.doc.setCharacterAttributes(1, 1,attr,false);

              //print out the text when we've removed bold and italic
              System.out.println(htmltest.jEditorPane1.getText());

              System.exit(0);
          }
      }


      Run as is, the output is exactly as expected. The "1" gets given bold and
      italic tags, and then has them removed. Now change the line that reads:
              Reader in = new java.io.StringReader("12345");
      to read
              Reader in = new java.io.StringReader("<b>1</b>2345");
      and run it again, and you'll see in the system out that the bold tag remains.
      (Review ID: 103900)
      ======================================================================

            svioletsunw Scott Violet (Inactive)
            skondamasunw Suresh Kondamareddy (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: