Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting custom font

I'm trying to set a custom font (bilboregular.ttf) to 2 jLabels in my program The fonts aren't being loaded successfully though.

Here is the main method calls:

//this should work if the build is in a jar file, otherwise it'll try to load it directly from the file path (i'm running in netbeans)
if (!setFonts("resources/bilboregular.ttf")) {
        System.out.println("=================FAILED FIRST OPTION"); // <<<<<<<< This is being displayed
if(!setFonts(System.getProperty("user.dir")+"/src/resources/bilboregular.ttf")){
            System.out.println("=================FAILED SECOND OPTION"); // <<< This is not being displayed
        }
    }

Here is the other method:

public boolean setFonts(String s) {
    try {
jLabel3.setFont(java.awt.Font.createFont(java.awt.Font.TRUETYPE_FONT, new java.io.File(s)));
jLabel4.setFont(java.awt.Font.createFont(java.awt.Font.TRUETYPE_FONT, new java.io.File(s)));
        return true;
    } catch (Exception ex) {
        return false;
    }
}
like image 703
mangusbrother Avatar asked Dec 05 '22 13:12

mangusbrother


2 Answers

Firstly gain an URL to the Font. Then do something like this.

'Airacobra Condensed' font available from Download Free Fonts.

Registered Font

import java.awt.*;
import javax.swing.*;
import java.net.URL;

class LoadFont {
    public static void main(String[] args) throws Exception {
        // This font is < 35Kb.
        URL fontUrl = new URL("http://www.webpagepublicity.com/" +
            "free-fonts/a/Airacobra%20Condensed.ttf");
        Font font = Font.createFont(Font.TRUETYPE_FONT, fontUrl.openStream());
        GraphicsEnvironment ge = 
            GraphicsEnvironment.getLocalGraphicsEnvironment();
        ge.registerFont(font);
        JList fonts = new JList( ge.getAvailableFontFamilyNames() );
        JOptionPane.showMessageDialog(null, new JScrollPane(fonts));
    }
}

OK, that was fun, but what does this font actually look like?

Display Font

import java.awt.*;
import javax.swing.*;
import java.net.URL;

class DisplayFont {
    public static void main(String[] args) throws Exception {
        URL fontUrl = new URL("http://www.webpagepublicity.com/" +
            "free-fonts/a/Airacobra%20Condensed.ttf");
        Font font = Font.createFont(Font.TRUETYPE_FONT, fontUrl.openStream());
        font = font.deriveFont(Font.PLAIN,20);
        GraphicsEnvironment ge =
            GraphicsEnvironment.getLocalGraphicsEnvironment();
        ge.registerFont(font);

        JLabel l = new JLabel(
            "The quick brown fox jumped over the lazy dog. 0123456789");
        l.setFont(font);
        JOptionPane.showMessageDialog(null, l);
    }
}
like image 130
Andrew Thompson Avatar answered Dec 30 '22 08:12

Andrew Thompson


  • don't load a new Font repeatly, for each of JLabel separatelly

means

public boolean setFonts(String s) {
    try {
jLabel3.setFont(java.awt.Font.createFont(java.awt.Font.TRUETYPE_FONT, new java.io.File(s)));
jLabel4.setFont(java.awt.Font.createFont(java.awt.Font.TRUETYPE_FONT, new java.io.File(s)));
        return true;
    } catch (Exception ex) {
        return false;
    }
}
  • create a Font(s) as Local variable(s) and to change only jLabel3.setFont(myFont), or register a new Font (see link from @StanislavLs comment)

for example

InputStream myFont = OptionsValues.class.getResourceAsStream(
     "resources/bilboregular.ttf");
like image 30
mKorbel Avatar answered Dec 30 '22 06:12

mKorbel