I'm working on a java app with a number of text fields and custom JComponents. So far, I have put all my text inside JLabels, and styled it with HTML, (using<font>
or <style>
). This also handles wrapping nicely.
However, I would like to put text at custom coordinates withing one of my custom JComponents. When I do this (using graphics.drawString(text,x,y)
), the HTML code seems to have no effect, so instead I tried styling it with g.setFont(new Font("Courier New", Font.PLAIN, 14)); g.setColor(new Color(0x999999));
This worked, but the text looked noticeably different, even in what should be the same font/color/style. It may be hard to see in the pictures I posted below, but the HTML styled code looks much smoother and a little thicker (and no, its not bold). (in this example, the swing text is actually size 14 instead of 12, but it looks the same either way);
Swing styled text:
html styled text:
My question is, basically, what should I do about this? I could go through my project and restyle all of the HTML strings with setFont()
, but that sounds like a lot of work, and I actually much prefer the HTML look. Is there a way to either:
a) style text so that it looks like the HTML text,
b) parse/use HTML styles in custom JComponents, maybe by extending JLabel and leveraging whatever internal API it uses
c) draw a new JLabel with the text I want to a specified set of coordinates (within a JPanel, etc.)
d) some other option?
A search of Google has revealed nothing useful. Please let me know what you think. Thank you.