4

I've overridden the paintComponent method of an extended JToggleButton so that I can use a TexturePaint fill for the text when the button is toggled. The problem I'm running into is that I can't seem to draw the text using the same font as my look and feel is using as a default. I've tried g2d.setFont(this.getFont()); , where "this" is the button I'm working with. The font is close, but appears bolder than the default text when I draw it. Is there a better way to draw text such that it looks the same as the default, save for the color? Thanks in advance!

Mike Daniels
  • 8,582
  • 2
  • 31
  • 44
bendicott
  • 369
  • 2
  • 17

2 Answers2

4

If you are overriding the paintComponent() method then the Graphics object should already be configured to have the Font of the toggle button. The difference is probably because of anti aliasing which is not turned on by default.

I have found some code that works for me in very limited testing. Try the following in the paintComponent() method:

Graphics2D g2 = (Graphics2D)g.create();
Toolkit toolkit = Toolkit.getDefaultToolkit();
Map map = (Map)(toolkit.getDesktopProperty("awt.font.desktophints"));

if (map != null)
{
    g2.addRenderingHints(map);
}

g2.drawString(...);
g2.dispose();

I was warned in this posting - How to set text above and below a JButton icon? - that this will not work on all platforms and LAF's. The comment also gives a suggested solution on how to paint the text.

Community
  • 1
  • 1
camickr
  • 321,443
  • 19
  • 166
  • 288
  • How would I go about painting a JLabel to a BufferedImage (as suggested by Kleopatra in your linked post)? – bendicott Nov 16 '11 at 22:20
  • You can use the [Screen Image](http://tips4java.wordpress.com/2008/10/13/screen-image/) class. Just wondering did you try the code I suggested? Did you have a problem? – camickr Nov 17 '11 at 04:12
  • Found a bug in my code; the code you posted does indeed work! Now I just need to position the text correctly and I'm good to go. Thanks for the help! – bendicott Nov 17 '11 at 18:44
  • ...and I got the text where it's supposed to be. Thanks again! – bendicott Nov 17 '11 at 19:06
  • What LAF and platform are you using? I want to keep track of when this code works and when it doesn't. Also, did you zoom in on the painted text to make sure it was exactly the same? – camickr Nov 17 '11 at 19:53
  • Metal LaF on windows 7, 64 bit. Yeah, the text matches perfectly. – bendicott Nov 18 '11 at 04:30
  • Thanks,I've only tested it on Windows XP using Metal and Windows LAF. I was hoping you where using another LAF or platform :) – camickr Nov 18 '11 at 06:22
3

This question is similar and provides an answer: How do I get the default font for Swing JTabbedPane labels?

I'm not quite sure what the key would be, but following that answer you may want to try:

UIManager.getLookAndFeelDefaults().getFont("ToggleButton.font");

EDIT

This is not the snippet from the linked question, but after a small bit of testing it appears to be equivalent to the:

UIManager.getDefaults().getFont("ToggleButton.font");

which is the code given in the linked question.

EDIT 2

I think I've found a solution. The default returned is just a plain font, I got around this in a sample with the line:

this.setFont(UIManager.getDefaults().getFont("ToggleButton.font").deriveFont(this.getFont().getStyle(), this.getFont().getSize()));

My suggestion (to make that not as ugly) is add some private properties for the default Font Style and Size to your class (and you can set them in the constructor):

fontStyle = this.getFont().getStyle();
fontSize = this.getFont().getSize();

And then you could clean up with:

this.setFont(UIManager.getDefaults().getFont("ToggleButton.font").deriveFont(this.fontStyle, this.fontSize));
Community
  • 1
  • 1
Brandon Buck
  • 7,177
  • 2
  • 30
  • 51