Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 583
Dewsworld Avatar asked Oct 21 '25 10:10

Dewsworld


2 Answers

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.

like image 119
Jon Bright Avatar answered Oct 23 '25 01:10

Jon Bright


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();
like image 39
KV Prajapati Avatar answered Oct 22 '25 23:10

KV Prajapati



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!