Monospaced font/symbols for JTextPane

user28061 picture user28061 · Jun 13, 2012 · Viewed 22.7k times · Source

I want to build a console-like output using JTextPane. Therefore I am using a monospaced font:

textpane.setFont(new Font(Font.MONOSPACED, Font.PLAIN, 12));

This works fine for all kind of alphanum (like a-z, 0-9 etc.) characters, but when it comes to symbols like '\u2588' (█), the font isn't monospaced anymore.

Did I forgot something? Or isn't there a monospaced font which includes smybols?

Answer

CodeBlind picture CodeBlind · Jun 13, 2012

Ok, first off, it sounds to me like you're trying to address a couple of different things here, so I'll try to address them both separately.

1. You need a font that is monospaced for all unicode characters, symbols or otherwise.

According to this page, there were 12886 alphanumeric and "symbol" characters defined by the Unicode 3.2 standard. Unicode is now at 6.0, so we can probably assume that number is larger now. I'm also assuming here that "alphanumeric" means English characters, because Unicode supports over 100000 characters spanning many languages. At any rate, 12886 English-recognized characters and symbols is still A LOT, and I doubt that there are many free fonts that support all of them.

That said, I end up using Courier New for most of my Java applications that need a mono-spaced font with character support. It supports the '\u2588' character you mentioned above as well as many other important ones like the "degrees" symbol.

2. This mono-spaced font needs to be "cross-platform"

I know for certain that Mac OS X and all flavors of Windows support Courier New, and the versions of Linux I run on do too (RedHat... can't remember the version number), although not all Linux versions have this font natively. Anyway... you might try Courier New and see if it works for you. If not, you can probably find free tools online for testing font/character support.

Final thoughts

I hate to say it, but I doubt that there are many fonts out there that support all alphanumeric/symbol characters defined by the Unicode standard, least of all being mono-spaced, cross-platform, and free. If possible, it might be more worth your time to try to figure out what symbols you will need for certain, then choose a font that supports those symbols and is in turn supported by the platforms you know you will run your application on. If you absolutely need to have support for all Unicode symbols, unfortunately, it probably won't come free.