Archived

This topic is now archived and is closed to further replies.

[java] Styling a JTextPane

This topic is 5626 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Im trying to write a program that will color code certain keywords, kinda like an IDE. Whats the best way to implement styling in a textpane? Currently I''m checking the entire document via a DocumentListener and removing/inserting strings with the correct styling. This is extremely cpu intensive and I was wondering if there was a better way or if anyone knew of a class someone has written that lets me do something like setWordStyle("cat","red");

Share this post


Link to post
Share on other sites
here is a suggestion: you could just check what the user changed.
let''s explain:
-when the user types a key, the word he is currently typing (from the next ''space'' on one side to the next ''space'' on the other) is checked, and changed if needed. Nothing is checked until the used press a key and only one word is checked.

This approach disable composite words (word ''space'' word). The last thing you must do is for the ''paste'' action. When the user do it, you must simply check the whole selection going from the previous bar position to the current bar pos, including the border words.

Some things to notice:
-when i say ''space'', i mean space , \n , \r and \t (i don''t know how they are expressed in java)
-when deleting, it''s not a problem, just check the word around your bar
-when some words must be set in one color, don''t forget that the opposite must also be done
-when the user makes a text selection and then paste something, you must compare the start point of the selection (not the bar position!) to the bar position after pasting the text.

Share this post


Link to post
Share on other sites