aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes change text color of highlighter Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "change text color of highlighter" Watch "change text color of highlighter" New topic
Author

change text color of highlighter

mark goking
Ranch Hand

Joined: Aug 18, 2009
Posts: 155
hi. anybody knows which class or which particular method needs to be overridden in order to change the text color of the highlighter in a jtextpane?

im doing this because if the highlighter's background color is black, the text color has to be white. currently it defaults to a gray color.

the HighlightPainter does not have any means to do this, only the color passed as the parameter to the super class will become the background color but if you set a color that is dark, the text foreground color does not change automatically. any thoughts?


Website/Java Games: http://www.chitgoks.com
Tech Blog: http://tech.chitgoks.com
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2213
    
    7


If you need more help then post your SSCCE demonstrating the problem.
mark goking
Ranch Hand

Joined: Aug 18, 2009
Posts: 155


please type in some text, then highlight it. the background color is black. id like to then change the text color to white for those highlighted areas. but the defaulthighlightpainter does not have any method to do so. any thoughts?
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2213
    
    7
Use attributes, instead of the highlighter:



Of course you would create the attributes once and just reuse them.
mark goking
Ranch Hand

Joined: Aug 18, 2009
Posts: 155
hi camick, that is actually my other alternative. thank you
Stanislav Lapitsky
Ranch Hand

Joined: Dec 01, 2009
Posts: 53
What about JTextComponent.setSelectedTextColor()?
Take a look at the GlyphView.public void paint(Graphics g, Shape a) method source


As you can see the color is used if the SwingUtilities2.useSelectedTextColor(highlight, tc) is true.
mark goking
Ranch Hand

Joined: Aug 18, 2009
Posts: 155
hi stan, that line needs a highlight object and my highlight object is actually the background color

i need the text color to be white if my highlighter color is dark.
Stanislav Lapitsky
Ranch Hand

Joined: Dec 01, 2009
Posts: 53
Actually foreground color (if specified) is used to render the text. May be you can try to set it before selection painting and reset after.
mark goking
Ranch Hand

Joined: Aug 18, 2009
Posts: 155
ok. one more thing, maybe you have an idea. i am doing multiple highlights using different painters (and colors), i want this certain painter object to always be on top and overlap even if there is already a highlight that exists

i was thinking of getting all highlights using jtextpane.getHighlighter().getHighlights() and sort the painter object that needs to be on top all the time to be always on the last of the array. any thoughts?
Stanislav Lapitsky
Ranch Hand

Joined: Dec 01, 2009
Posts: 53
I would say you need one highlighter/painter which will decide how to draw particular highlight. Also it should manage all the highlights addings/removing and merge/split highlights if necessary. I don't think overlaping highlights is a good idea. If you have the overlap just break existing highlights or create a migger one and remove inner highlights.
mark goking
Ranch Hand

Joined: Aug 18, 2009
Posts: 155
man .. DefaultHighlighter's paintLayeredHighlights() paints highlights in reverse order, that's why the newest highlights do not overlap the old ones.
mark goking
Ranch Hand

Joined: Aug 18, 2009
Posts: 155
i copied the source code of DefaultHighlighter and overrode the paintLayeredHighlights() to render the highlights according to when they were added. this way, any new highlight will always be on top of old highlights. lolz. first time i appreciated an IDE's usefulness ha ha!
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2213
    
    7
How does using painting the highlight in the order they where added solve the problem? Don't you still have the problem of the text painting in the wrong color? Post your solution so others can benefit. I'm curious to see the result.
mark goking
Ranch Hand

Joined: Aug 18, 2009
Posts: 155
@rob: my previous post did say that it is my other alternative. so that is what i did but i used highlights to paint the background
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2213
    
    7
So you are using a highlighter for the background and attributes for the foreground. Why wouldn't you use attributes for both. That way you don't need a custom painter to paint the highlights in reverse order.
mark goking
Ranch Hand

Joined: Aug 18, 2009
Posts: 155
ok
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: change text color of highlighter