Search code examples
javajtextpanerenderer

JTextPane: What's the renderer of/for setCharacterAttributes?


I have a JTextPane in my application and I prepare some styles for various words/letters. These styles are applied via setCharacterAttributes. Now I plan to style the background of some styles: If a style defines a background (say gray) and I want to soften the corners (round corners with ie. 3px).

Is there a way to add a special renderer that is used within setCharacterAttributes? Or, do you recommend HighlightPainter added via pane.getHighlighter().addHighlight(...)?


Solution

  • Ok, Your question is not very clear, but if I understand correctly, you intend to customize the default behaviour of the background properties on characters. You describes 2 approaches, and one is probably much easier than the other one.

    1) Based on Character attributes (complex one) : You need to define your behaviour by overriding the paint method in javax.swing.text.GlyphView. Then, you will need to change the ViewFactory of your EditorKit to make it take your change into account. I would not recommend this approach.

    2) Based on Highlights (easier one) : You need to define a new javax.swing.text.Highlighter.HighlightPainter that paints the round borders as you wish. Then you need to find every set of text where a background is set. You remove the background and add your custom highlighter instead. You can optimize the process, but I think you already got this part.