Dewsworld
Dewsworld

Reputation: 14033

Java Font problem

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.

Upvotes: 4

Views: 3526

Answers (2)

Jon Bright
Jon Bright

Reputation: 13738

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.

Upvotes: 5

KV Prajapati
KV Prajapati

Reputation: 94645

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();

Upvotes: 4

Related Questions