File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Syntax highlighting in JTextPane Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Syntax highlighting in JTextPane" Watch "Syntax highlighting in JTextPane" New topic
Author

Syntax highlighting in JTextPane

Hunter McMillen
Ranch Hand

Joined: Mar 13, 2009
Posts: 492

Hey everyone,

I was hoping to get some suggestions on how to go about highlighting individual words in a JTextPane, similar to how an IDE would highlight programming language keywords. The way I am doing it right now doesn't seem very good, I check every word to see if it is a keyword then append it to the JTextPane which a specific color. But to do this I have to divide the line into words which is ruining all of the formatting.



I am using a enumeration to hold all of the words that are keywords:


Any ideas on better ways to do this would be great.

Thanks.
Hunter

"If the facts don't fit the theory, get new facts" --Albert Einstein
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12778
    
    5
I would certainly put the keywords in a Map for use in your



method, that is exactly the kind of use that Maps are designed for.

Iterating the whole list has got to be slowing you down.

Bill
Hunter McMillen
Ranch Hand

Joined: Mar 13, 2009
Posts: 492

Thank you, not sure why I didn't consider that as a solution. My current version actually runs really fast because I test the line for keywords before I perform any splitting or other manipulation, and since there aren't keywords on every line of code it seems to be working well at the moment. I don't believe that it will scale very well, and you are right with enough keywords it will eventually slow me down.

Thanks,
Hunter
Hunter McMillen
Ranch Hand

Joined: Mar 13, 2009
Posts: 492

I am trying to add the map you suggested to my enum class, but I am receiving an odd error.



It makes since to me for this map to be static because I am only using it internally inside the enum class and don't want to create a new enum object to use it.
any ideas?

Hunter
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19692
    
  20

The reason is because of the way enums work. Their constants are created before anything else. That means that this constructor will be called before the map is created. You can solve this by using lazy initialization:


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Hunter McMillen
Ranch Hand

Joined: Mar 13, 2009
Posts: 492

ahh I see thanks Rob.

Hunter
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19692
    
  20

You're welcome.
Hunter McMillen
Ranch Hand

Joined: Mar 13, 2009
Posts: 492

hmm I am switching to use lazy initialization like you suggested but the compiler is still showing an error "illegal reference to static field from initializer"


I ended up having to use a static inner class to compile the code:



Hunter
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19692
    
  20

Apparently the static declaration cannot be used at all inside the constant constructors. But I must say you've found an excellent solution.
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 3014
    
  10
Another possibility:

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Syntax highlighting in JTextPane