🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.jbuilder.java.swing
Article #8444

setCharacterAttributes in HtmlDocument jdk 1.2.2 : Can't build a LeafElement

#8444
From: Fran�ois B�lair
Date: Sat, 19 Aug 2000 00:00
88 lines
3464 bytes
Salutation,

I have a JTextPane using a content type "text/html". I want to be able to
modify the style of some text inside the JTextPane.

Basically what i have done is this :

1- I load a html page in the JTextPane. The page is correctly displayed.

    JTextPane textPane = new JTextPane();
    textPane.setPage(url);

2- I create two new contextStyle : one is the default, the other is an
highlight.

  StyleContext sc = new StyleContext();
  Style mainStyle;
  Style selectedStyle;
  setStyle(sc);

  protected void setStyle( StyleContext sc)
  {
    // Create and add the default style
    Style defaultStyle = sc.getStyle(StyleContext.DEFAULT_STYLE);
    mainStyle = sc.addStyle("MainStyle", defaultStyle);
    StyleConstants.setFontFamily(mainStyle, "Serif");
    StyleConstants.setForeground(mainStyle, Color.black);
    StyleConstants.setFontSize(mainStyle, 14);

    // Add the selection style
    selectStyle = sc.addStyle("ConstantWidth", null);
    StyleConstants.setFontFamily(selectStyle, "Monospaced");
    StyleConstants.setForeground(selectStyle, Color.blue);
    StyleConstants.setFontSize(selectStyle, 14);
}

3- Now i try to apply a selection to the text

    // Test the current update text
    updateText(textPane, selectStyle, 70, 10);
  protected void updateText(JTextPane textPane, AttributeSet
activeAttribute, int offset, int length)
  {
    // Get the format
    String contentType = textPane.getContentType();

    if( contentType == "text/html" )
    {
      HTMLDocument htmlDoc = (HTMLDocument) textPane.getDocument();
      if( htmlDoc != null && offset >= 0 && length > 0 )
      {
         htmlDoc.setCharacterAttributes(offset, length, activeAttribute,
false);
      }
    }
  }

Result :

Under jdk 1.3 this code works like a charm. The selected portion of the text
gets the blue color.

Under jdk 1.2.2 and below (jdk 1.1.x) i get this exception :

"java.lang.Error: Can't build a LeafElement(ConstantWidth) 70,80"
at
javax.swing.text.html.HTMLEditorKit$HTMLFactory.create(HTMLEditorKit.java:93
4)
 at
javax.swing.text.ParagraphView.updateLogicalChildren(ParagraphView.java:853)
 at javax.swing.text.ParagraphView.changedUpdate(ParagraphView.java:922)
 at javax.swing.text.html.ParagraphView.changedUpdate(ParagraphView.java:94)
....
at java.awt.EventQueue.dispatchEvent(EventQueue.java, Compiled Code)
 at java.awt.EventDispatchThread.pumpOneEvent(EventDispatchThread.java,
Compiled Code)
 at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:92)
 at java.awt.EventDispatchThread.run(EventDispatchThread.java:83)


Any idea why it wouldn't work in jdk 1.2.2 but would in jdk 1.3 ?

All i can see is that the AbstractDocument.LeafElement is screwed.
Don't know why though.

Thank you in advance,

Francois Belair
Technical Details
Message-ID: <8nmbks$b2b2@bornews.borland.com>
Path: arc1-ca.pugleaf.net!unknown!not-for-mail

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: 111d45,66d26cb26de91cdf,start
X-Google-Attributes: gid111d45,public
From: "Fran�ois B�lair" <belair@orbitek.com>
Subject: setCharacterAttributes in HtmlDocument jdk 1.2.2 : Can't build a LeafElement
Date: 2000/08/19
Message-ID: <8nmbks$b2b2@bornews.borland.com>
X-Deja-AN: 660116241
X-Priority: 3
X-MimeOLE: Produced By Microsoft MimeOLE V5.00.2919.6600
Organization: Another Netscape Collabra Server User
X-MSMail-Priority: Normal
Reply-To: "Fran�ois B�lair" <belair@orbitek.com>
Newsgroups: borland.public.jbuilder.java.swing