Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

change text color of highlighter

 
mark goking
Ranch Hand
Posts: 155
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Rob Camick
Ranch Hand
Posts: 2559
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


If you need more help then post your SSCCE demonstrating the problem.
 
mark goking
Ranch Hand
Posts: 155
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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
Posts: 2559
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Use attributes, instead of the highlighter:



Of course you would create the attributes once and just reuse them.
 
mark goking
Ranch Hand
Posts: 155
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi camick, that is actually my other alternative. thank you
 
Stanislav Lapitsky
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 155
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 155
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 155
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 155
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2559
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 155
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@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
Posts: 2559
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 155
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic