This week's book giveaway is in the Design forum.
We're giving away four copies of Design for the Mind and have Victor S. Yocco on-line!
See this thread for details.
Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Syntax highlighting in JTextPane

 
Hunter McMillen
Ranch Hand
Posts: 492
Firefox Browser Linux VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13058
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 492
Firefox Browser Linux VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 492
Firefox Browser Linux VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20510
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:
 
Hunter McMillen
Ranch Hand
Posts: 492
Firefox Browser Linux VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ahh I see thanks Rob.

Hunter
 
Rob Spoor
Sheriff
Pie
Posts: 20510
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome.
 
Hunter McMillen
Ranch Hand
Posts: 492
Firefox Browser Linux VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20510
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3032
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Another possibility:

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic