This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
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.
"If the facts don't fit the theory, get new facts" --Albert Einstein
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.
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: