🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

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

Help with Swing text components.

#7467
From: Paul Sprakes
Date: Tue, 23 Jan 2001 12:28
54 lines
3065 bytes
This is my first foray into Swing and I am having a few problems.

I followed the text editor tutorial in the JBuilder 4 foundation help files
and I am now trying to extend it into a syntax highlighting editor for JSP
files. So far I have changed the text component from a JTextArea to a
JEditorPane and created my own Document (JSPStyledDocument) which extends
DefaultStyledDocument and assigned this to the JEditorPane. Within
JSPStyledDocument I have overridden the insertString method which simply
calls super.insertString() and then calls my own parse function which
follows:

private void parse() {
    String content = "";
    try {
      content = getText(0, getLength());    //gets all the panes content
into a string
      int start = 0;
      start = content.indexOf("test");    //find occurence of "test"
      System.out.println("Found test: " + start);    //debug
      SimpleAttributeSet redAttr = new SimpleAttributeSet();    //this is to
highlight the text
      if(start != -1) {    //we found an occurence
        StyleConstants.setForeground(redAttr, new Color(255,0,0));
        StyleConstants.setBold(redAttr, true);
        System.out.println("removing from " + start + " to " + (start + 4));
//debug stuff
        remove(start, 4);    //remove the occurence of "test"
        super.insertString(start, "test", redAttr); //replace it with "test"
plus a new attribute (red + bold)
        //setCharacterAttributes(start, end, redAttr, false);    //1st try.
This didn't work
      }
    } catch(BadLocationException e) {
        System.out.println(e);
    }
  }

//---------------------------end-parse()-------------------------

Everything compiles and runs OK except the attributes don't change -- when i
type "test" it stay's in the same font color and style. I know it replaces
the text as if I modified the string in the super.insert call, this would
appear in the JEditorPane.

Any ideas?

Secondly, on my first try I used setCharacterAttributes() but this gave me
an error: InvalidStateException     error rendering 0,32. Why?

TIA.

Paul Sprakes.

P.S. I know the above isn't the ideal way to do this, but it's a start!
Technical Details
Message-ID: <94jtcq$19c23@bornews.inprise.com>
Path: arc1-ca.pugleaf.net!supernews.google.com!sn-xit-02!supernews.com!isdnet!130.59.211.10.MISMATCH!merapi!news-zh.switch.ch!newsfeed-zh.ip-plus.net!news.ip-plus.net!bornews.borland.com!not-for-mail

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: 111d45,f38fd383305363ca,start
X-Google-Attributes: gid111d45,public
X-Google-ArrivalTime: 2001-01-23 04:30:40 PST
Path: arc1-ca.pugleaf.net!supernews.google.com!sn-xit-02!supernews.com!isdnet!130.59.211.10.MISMATCH!merapi!news-zh.switch.ch!newsfeed-zh.ip-plus.net!news.ip-plus.net!bornews.borland.com!not-for-mail
From: "Paul Sprakes" <paul@sprakes.co.uk>
Newsgroups: borland.public.jbuilder.java.swing
Subject: Help with Swing text components.
Date: Tue, 23 Jan 2001 12:28:24 -0000
Organization: Another Netscape Collabra Server User
Lines: 56
Message-ID: <94jtcq$19c23@bornews.inprise.com>
NNTP-Posting-Host: ppp-3-232.5800-12.access.uk.worldonline.com
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 5.50.4133.2400
X-MimeOLE: Produced By Microsoft MimeOLE V5.50.4133.2400