File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Swing / AWT / SWT and the fly likes Preventing inserted text from becoming colored from previous style Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Preventing inserted text from becoming colored from previous style " Watch "Preventing inserted text from becoming colored from previous style " New topic
Author

Preventing inserted text from becoming colored from previous style

Khoa Nguyen
Greenhorn

Joined: Aug 08, 2007
Posts: 5
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

Joined: Jan 14, 2004
Posts: 1535
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

Joined: Sep 20, 2005
Posts: 862
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:



bitguru blog
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Preventing inserted text from becoming colored from previous style
 
Similar Threads
How to set different color for the different text in a JTextArea
font color in JTextArea
Need help in saving the diff colored text in JTextPane in diff format
Printing to something besides the console
JTextPane "lag" (not time issue, color issue)