I can’t reproduce the problem through the code you presented.
As pointed out by re22, it seems that the problem is exception treatment - your code works.
Follow a sample (note that it is not exactly "correct", many details have been omitted to reduce the code):
import java.awt.*;
import java.io.File;
import java.io.IOException;
import javax.swing.*;
public class TesteFonte extends JPanel {
JTextArea textArea;
String texto = "Testando uma fonte diferente!";
public TesteFonte() throws Exception {
super(new GridBagLayout());
textArea = new JTextArea(5, 20);
Font minhaFonte = Font.createFont(Font.TRUETYPE_FONT,
new File("C:\\Users\\Daniel\\Downloads\\oliver__.ttf"))
.deriveFont(Font.PLAIN, 28);
GridBagConstraints c = new GridBagConstraints();
c.gridwidth = GridBagConstraints.REMAINDER;
c.fill = GridBagConstraints.BOTH;
c.weightx = 1.0;
c.weighty = 1.0;
add(textArea, c);
textArea.setFont(minhaFonte);
textArea.append(texto + "\n");
}
private static void createAndShowGUI() throws Exception {
JFrame frame = new JFrame("Teste de Fonte");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new TesteFonte());
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
try {
createAndShowGUI();
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
}
Here is the result:
I suggest that you include your entire code in the question - if it is too big, that you try to reduce it, but so that it can still be executed. So experienced programmers (not my case), will solve cases like this in the blink of an eye!
Original code: Javadocs
Code to insert source: Soen (Neil Derno)
Font used in example text: "Oliver" (dafont)
"Default constructor cannot Handle Exception type Ioexception thrown by implicit sup...".
– Renan Gomes
Check if this is what you need http://stackoverflow.com/a/5652385/3792998
– jsantos1991