Default Fonts in Java - 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"
???

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.

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

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

Related

Font glyph fallthrough in Java

Does anyone know of an existing solution for font glyph fallthrough in Java? For example, our designers have decided that Calibri is the font that mostly fits our needs, but if I specify Calibri, it can naturally not render characters that do not have a matching glyph in that font. In that case, I would need it to fall through to a second specified font, and if all else fails - use one of Java's logical fonts.
Has anyone come up with a solution for this, which can be plugged into existing Swing components without having to write custom Swing components for the entire project?
This is a very old project already, and building custom graphical components is not a feasible solution.
This isn't a code-based solution and probably won't be of much help, since it requires each user to install a file locally, but just in case...
You can add fallback fonts in a special directory within the JRE installation. From the Java documentation:
Users can add a physical font as a fallback font to logical fonts used in Java 2D rendering by installing it in the lib/fonts/fallback directory within the JRE.

Eclipse Default Font name

What's the default font that Eclipse uses for Java code?
The default font Eclipse uses for (Java) code is Monospace.
The default font style eclipse is using is Consolas however Courier New is also a good option to go with.
Depending on your Eclipse version, the font might differ. Go to Window-> Preferences->General->Appearance->Color and Fonts. Choose Basic->Text Font and you can hit the Edit button to change font type and font size. It will be used by several other properties below that default to this font.

eclipse default font in ubuntu 12.04

I have asked this question on askubuntu thinking that it may be related to ubuntu but now that I think about it, it may rather be related to eclipse so I wanted to ask in here as well.
I'm using eclipse on my ubuntu 12.04 system. Currently the default font in eclipse is set to monospace (mostly) although my system monospace font defaults to ubuntu mono (checked from gnome-tweak-tool). I have found this in the eclipse documentation where it says
By default, the Workbench uses the fonts and colors provided by the operating system. However, there are a number of ways that this behavior can be customized.
I was wondering why eclipse is not using my system fonts. I can change the font from the preferences but there are a lot of different font settings for different places and I am using a few different eclipse's on my system so it would be nice to find something that would work globally.

Can I set the DPI resolution of my Java Swing application without changing the systems' DPI setting?

I have a Java application using the Substance LookAndFeel with Windows as the the target platform and I want to increase the DPI setting of my application without changing the system setting.
I want to do this because I don't want to force the user to restart Windows and because many Windows applications seem to have problems with very high DPI settings (> 120)
PS: I'm aware that the Substance LaF allows to scale the font size at runtime, but that way only the height of my controls are scaled, not the width. I want my GUI fully scaled as it would happen if I set the system's DPI setting.
Don't know if that is possible. The look&feel would have to support it, and as far as I know, the Windows Look&Feel does not. Here's a hack which you may consider: Iterate through all the fonts defined in your look&feel and redefine them to be slighly bigger. Here is a code snippet that does this:
for (Iterator i = UIManager.getLookAndFeelDefaults().keySet().iterator(); i.hasNext();) {
String key = (String) i.next();
if(key.endsWith(".font")) {
Font font = UIManager.getFont(key);
Font biggerFont = font.deriveFont(2.0f*font.getSize2D());
// change ui default to bigger font
UIManager.put(key,biggerFont);
}
}
I suppose you could take this one step further and redefine scale borders proportionally as well, but that gets very complicated very quickly
So the actual answer seems to be: no you can't. That really is a bummer because it's a pain to test.
Yes you can, but you need to run it on JRE 9.
This is because the Java runtime declared itself to be "DPI-aware" but didn't really supported it for AWT and Swing. Java applications were sized and rendered based on pixels rather than being properly scaled, this included HiDPI displays.
Anyways, this has been recently solved.
See the issue JEP 263: HiDPI Graphics on Windows and Linux
and the upgrade.
So, increasing the font size does not work (because it does not increase the rest of the things); the jvm argument -Dsun.java2d.dpiaware=false does not work (because it was not really supported); and the manifest file + registry edit (for Windows) just does not work.
Solution: You need to run it on JRE 9 because it really supports this feature.

Java Locale Font question..?

[EDIT] this happend on OSX with Java 1.5! (but may also happen on Windows!)
It seems japanese users of my Java Swing program cannot write japanese symbols in JTextFields. Actually they can write them but do not see them. They only see blocks which somehow indicates I think that the font does not support these symbols.
I set my own font for various reasons ("Lucida Sans", Font.PLAIN, 12) - which I wouldn't like to change. My questions are:
would the JTextFields show japanese Symbols without me setting this Font?
could I detect which Locale's are fully supported by my Font and only set it in those cases but leave the standard Java font for all other cases like Locale.Japan?
do you have any other suggestions?
Thank you for your help!
would the JTextFields show japanese Symbols without me setting this Font?
Most likely since the JRE will choose the default.
could I detect which Locale's are fully supported by my Font and only set it in those cases but leave the standard Java font for all other cases like Locale.Japan?
Yes, in theory, although easier would be to test if this font can display a locale specific character using the the canDisplay(char) method in Font. If it don't, you may switch to the default.
do you have any other suggestions?
Here's a link on this topic that may help
Do you have a link for your product?
For this very reason, my internationalized application sets its font depending on the locale. To answer your questions:
This probably depends on which version of Windows is being used. Is this the US edition with Japanese fonts installed, or is this the version sold in Japan? I don't know the exact answer to your question, but I expect that the answer is, "It depends."
I don't know of any standard way to do this. I have found a wonderful utility for investigating this kind of thing: BabelMap. This will tell you exactly what characters are supported by any given font.
Other suggestions: Set your font depending on Locale, at least for select Locales.

Categories