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!
-
For better help sooner, post an [SSCCE](http://sscce.org/). – Andrew Thompson Nov 17 '11 at 02:27
2 Answers
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.
-
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
-
-
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
-
-
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
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));

- 1
- 1

- 7,177
- 2
- 30
- 51
-
-
@bendicott Are you using Metal as your look and feel for this project? – Brandon Buck Nov 16 '11 at 19:26
-
yes, but I'm not sure that will always be the LaF. I also tried g2d.setFont(MetalLookAndFeel.getControlTextFont()); , but no improvement. – bendicott Nov 16 '11 at 19:27
-
@bendicott If you will not always have the MetalLaF there is no reason to call it explicitly like that anyways. – Brandon Buck Nov 16 '11 at 19:36
-
-
-
@bendicott That's all I can offer without seeing any of what you are doing. – Brandon Buck Nov 16 '11 at 20:12