javasynth

Synth override text drawing on button


Is it possible to use painter or some other technique to customize text drawing on controls with synth?

I know you can implement paintButtonBorder and paintButtonBacground in SynthPainter, but I was wondering is there a way to do it with text? Maybe some workaround?


Solution

  • Typically you would create a SynthGraphicsUtils subclass, override paintText(SynthContext ss, Graphics g, String text, Icon icon, int hAlign, int vAlign, int hTextPosition, int vTextPosition, int iconTextGap, int mnemonicIndex, int textOffset) and return an instance of your subclass from your button SynthStyle's getGraphicsUtils(SynthContext context) method.

    Anyway, if you are going to delegate painting to the super implementation, be wary of certain details if text is in HTML (begins with the <html> tag):

    1. The super implementation will paint ss.getComponent().getText() instead of text.
    2. The super implementation will manually set the text color before painting. For unstyled text, this will be ss.getComponent().getForeground(). For styled text, this will match the color specified in HTML/CSS.