1

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: java font style

html styled text: html

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.

QuinnFreedman
  • 2,242
  • 2
  • 25
  • 42

2 Answers2

2

a) style text so that it looks like the HTML text,

Try using anti-aliasing on your custom drawString graphics calls.

b) parse/use HTML styles in custom JComponents, maybe by extending JLabel and leveraging whatever internal API it uses

You could check the source code of the JCL and dig deep and rewrite all the same logic to make your graphics drawing logic works the same as the HTML rendering logic... but that's too much effort.

c) draw a new JLabel with the text I want to a specified set of coordinates (within a JPanel, etc.)

You can with a null layout.

d) some other option?

Just use the same method of choosing Fonts across your application. Perhaps store a static reference somewhere to the Font object used and use that object everywhere. This would mean not using the font html or css attributes and possibly extending JLabel to use anti-aliasing.

NESPowerGlove
  • 5,496
  • 17
  • 28
  • Thanks. Manually setting anti-aliasing helped. (although oddly none of the available methods were an exact match.) It's close enough though for what I'm doing. – QuinnFreedman Aug 12 '14 at 05:52
1

Swing UI delegates for text components typically specify the same font as used for logical font families. You may get better results using Font.SERIF, for example, or label.getFont().deriveFont(…), for example.

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045