Hi,

We are facing font issue in our java swing application in linux. Kindly help to solve issue. In our application we are explicity setting font as 'Arial' for labels and others in swing Application. Since Arial is not available in linux so it picks 'Lucida Sans Regular' as default font of java due to font fallback mechanism. But as we upgrade our jdk version from 1.5 to 1.7, we are facing the issue with display in our application. It is now picking up 'Deja vu' as default font. Due to this our application is not displaying correctly in linux.

Can somebody help to set font from specific directory. I am planning to put font in one of our application directory so that it will pick font from directory. I don't want to modify all java files where we need to replace all instance of 'Arial' to 'Lucida Sans Regular'. There might be some way in java application to set its own font fall back mechanism.

Kindly help.

Um, check if Arial exists and if not set something else?

Currently on linux, there is no arial font. So with java fallback mechanism it picks lucida earlier. But now it is not picking up. We don't want to modify all files. So we want to customized fall back mechnaism of font in java.

There are a gazillion X11/postscript fonts, including arial. for Linux. You just need to install them. Look at your package manager to find them. Just because they aren't installed by default doesn't mean they aren't there or available to be downloaded as needed. There are also bitstream fonts as well. Time to do some internet searching...

FWIW, arial is a poor rendition of the classic Helvetica font, which would be preferable to use.

I assume that the real issue here is that you cannot ensure that Arial (or any other given font you may choose) will be installed on the host system, correct? Unlike, say, Windows, or MacOS, there are no 'standard' fonts for Linux, which is what the real issue is IIUC.

Different distros have different base font sets, and no one set is to be found in all possible cases. Indeed, you cannot be certain that there are any software fonts at all - it is possible, though rare today, for a Linux system to run entirely in console mode with the firmware fonts. The best you can expect is that if the system has an X server, it will have the X11 core Type 1 fonts.

Fortunately, there are a few options. One is to check the system the application is running on, and use one of the default fonts that you can be certain are on the system. This presents a problem, though, as the default fonts are almost never acceptable quality, especially the X11 fonts. The next is to have a list of commonly used fonts which the application can choose from; however, a list that is comprehensive across different distros is likely to be prohibitively long. Next is to allow the user to choose the font they want it to be rendered with, but this opens up a huge can of worms. You can be draconian and force the user to install the font in question in order to run the program. You can even set up a font server that the program can download your choices of fonts from.

The real solution? Add the font file as a resource inside the JAR you use to distribute the application. If you do this, though, I would avoid usinga copyrighted font, for obvious reasons. There are plenty of free TrueType and OpenType fonts for you to choose from, so selecting one that is freely distributable shouldn't present any trouble.

Like I said, check that the font you want exists, and, if not, use another.

java.awt.GraphicsEnvironment.getAvailableFontFamilyNames();

Member Avatar for iamthwee

I'd go with deploying the app in a jar file as well with font supplied.

I can understand the need to not edit all your sources.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.