This week's book giveaway is in the Other Languages forum.
We're giving away four copies of Functional Reactive Programming and have Stephen Blackheath and Anthony Jones on-line!
See this thread for details.
Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Preventing inserted text from becoming colored from previous style

 
Khoa Nguyen
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a jtextpane I have alreadly colored in black, and red. Red is for the text inside the brackets. This seems to work, but the problem is when I insert text imediately next to a red bracket, the next text that comes out is red as well, while it should be black. Right now what I'm doing to fix the problem is coloring everything black first then color in the red, but that makes the jtextpane blinks between two colors. I was wondering if there is some simpler way to prevent the style from being "contagious".

thanks in advance.
 
Craig Wood
Ranch Hand
Pie
Posts: 1535
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Testing indicates that text added (by typing or pasting) within the style run takes on the red color and text added outside the run does not. The space immediately after the run appears to be counted as part of the run, ie, the beginning of the whitespace. The space after this is not, ie, the beginning of the next word.
 
Brian Cole
Author
Ranch Hand
Posts: 908
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by khoa nguyen:
I have a jtextpane ... but the problem is when I insert text imediately next to a red bracket, the next text that comes out is red as well, while it should be black.


One way to handle this is to override the JTextPane's replaceSelection() method:

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic