Question

When I try to set a JLabel's font by a font that is returned from Font.createFont, JLabel displays nothing:

Font tempFont = Font.createFont(Font.TRUETYPE_FONT, new File("Path to a ttf font"));
Font font =  tempFont.deriveFont(48);
JFrame frame = new JFrame();
JLabel text = new JLabel("Salam Jahan!");
text.setFont(font);
frame.getContentPane().add(text);
frame.pack();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);

What is the problem?

Was it helpful?

Solution

deriveFont specifys the font style rather than the size so the loaded font retains its default size of 0

Font font = tempFont.deriveFont(48);

You want the overloaded version of the method

Font font = tempFont.deriveFont(48f);
                                  ^
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top