Troy Nichols
Troy Nichols

Reputation: 1234

Default Fonts in Java

I have some Swing code (written in 1.6 for 1.6) that specifically sets the font in a text area to Courier, but on some Windows systems, this shows up as Arial (the system default?) instead. Does that mean a font is missing from the system? What is the behavior Java has when it can't find a font it is looking for? Does it complain? Does it log it somewhere? Does it immediately resort to using the system default? Is the behavior different between 1.4/1.5/1.6 versions of the JVM?

Has anyone else ever run into this? I was very surprised to have something different from what I HARDCODED into the application show up in the UI - and only on some systems. The core issue is that I need a monospaced font style for this particular case, and Arial is not monospaced. Is there some way to specify a fallback if a certain font is not found? Something like:

if font is available use "Courier" else use "monospaced"

???

Upvotes: 0

Views: 3291

Answers (3)

MiguelMunoz
MiguelMunoz

Reputation: 4914

It might expect you to specify "Courier New" for Courier. I don't have a Windows system, so I can't verify this.

Upvotes: 0

Green Jalapeno
Green Jalapeno

Reputation: 13

According to this article1 the only monospaced TrueType fonts shipped by Microsoft are Courier New and Lucida Sans Typewriter.

Upvotes: 1

Martin Blech
Martin Blech

Reputation: 13543

You should first check if "Courier" is among the results of GraphicsEnvironment.getAvailableFontFamilyNames()

I don't know any built-in mechanism in Java for "if-unavailable-fallback-to" behavior.

Upvotes: 1

Related Questions