Say I have a Swing JComponent and I set a Font for the text of that JComponent. I build the project and create a .jar file of my project. Now, If I run this jar file from another computer where the Font is not install, what will be happen? Will the jar automatically install the Font or I need to make some kind of checking to do that? Thank you.
Asked
Active
Viewed 3,158 times
3
-
2I'm not sure about the detection side of it, but you may find [this method](http://download.oracle.com/javase/6/docs/api/java/awt/Font.html#createFont%28int,%20java.io.File%29) useful for the loading side of things, if you're not already using it! – obfuscation Sep 18 '11 at 11:32
-
It worked! But how can I set the font-size, then. – Dewsworld Sep 19 '11 at 15:00
2 Answers
4
If you need to use that specific font, you'll have to make sure it's installed on the machine before your jar file is run, or load it yourself. There's nothing that will automatically install the font for you. You can retrieve the list of available fonts using this code:
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
String [] fonts = ge.getAvailableFontFamilyNames();
Alternatively, you can load and register the font yourself using
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
Font f = Font.createFont(Font.TRUETYPE_FONT, new File(pathToYourTTFFile));
ge.registerFont(f);
You should check the return code of registerFont
and catch/deal with the exceptions thrown by createFont
.

Jon Bright
- 13,388
- 3
- 31
- 46
3
No there isn't any automatic method. You have to install font manually however you may get the available fonts using,
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
String[] names = ge.getAvailableFontFamilyNames();

KV Prajapati
- 93,659
- 19
- 148
- 186
-
It works fine, thanks. However when creating PDF [microsoft_pdf/xps] invoking print() method after registering it, PDF shows check boxes instead of real character. Help me. – Vivek Vadoliya Nov 19 '21 at 06:16