Skip to main content
GameDev.net gamedev.net
🔒 Locked

[java] Styling a JTextPane

Started by Useful Jul 23, 2002 at 7:01 AM 1 replies 350+ views
Original Post
Useful
Useful
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");
misterX
misterX
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.
misterX
misterX
aren''t already such text editors existing? I think a little google would be usefull.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.