I'm loading a custom font that is saved in the project folder as a ttf file. I get no errors when loading the font and when I print call the toString() method on plainFont it shows. [family=PT Sans,name=PT Sans Bold,style=plain,size=12]
private static Font plainFont;
private static Font boldFont;
private final JPanel openingScreen;
private final JPanel monthlyScreen;
private final JPanel addAOTMScreen;
private final JPanel viewAOTMScreen;
private final JPanel viewPortScreen;
private final JPanel screens = new JPanel(new CardLayout());
private final JScrollPane scrollPane;
/**
* Tracks various music related things, including an artist of the month tracker
*
* @author Matt Babel
*/
public MusicDatabase() {
setTitle("Music Database");
setLocation(400, 150);
setSize(500, 400);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setResizable(false);
setVisible(true);
File fontFile = new File ("PT_Sans-Web-Bold.ttf");
Font font = null;
try {
font = Font.createFont(Font.TRUETYPE_FONT, fontFile);
font = font.deriveFont(Font.PLAIN, 12);
} catch (FontFormatException e) {
System.err.println("Font is null");
System.exit(1);
} catch (IOException e) {
System.err.println("Font is null");
System.exit(1);
}
plainFont = font;
System.out.println(plainFont);
openingScreen = new JPanel();
monthlyScreen = new JPanel();
viewAOTMScreen = new JPanel();
addAOTMScreen = new JPanel();
viewPortScreen = new JPanel();
viewPortScreen.setLayout(new BorderLayout());
scrollPane = new JScrollPane();
scrollPane.getViewport().add(viewAOTMScreen);
scrollPane.getViewport().setBackground(backgroundColor);
viewPortScreen.add(scrollPane, BorderLayout.CENTER);
createOpeningScreen();
screens.add(openingScreen, "Opening Screen");
screens.add(monthlyScreen, "Monthly Screen");
screens.add(viewPortScreen, "View Screen");
screens.add(addAOTMScreen, "Add Screen");
add(screens);
CardLayout cardlayout = (CardLayout) screens.getLayout();
cardlayout.show(screens, "Opening Screen");
}
I use the setFont() method on monthlyButton but when the program starts the buttons text is the still the default text. I have no idea why this is happening.
/**
* initializes the opening screen
*/
public void createOpeningScreen() {
openingScreen.setBackground(backgroundColor);
openingScreen.setLayout(new GridBagLayout());
Button monthlyButton = new Button("Artists of the Month");
System.out.println(plainFont);
monthlyButton.setFont(plainFont);
monthlyButton.setPreferredSize(new Dimension(150, 150));
monthlyButton.addActionListener((java.awt.event.ActionEvent e) -> {
if (!monthlyScreenOn) {
createMonthlyScreen();
monthlyScreenOn = true;
}
CardLayout cardLayout = (CardLayout) screens.getLayout();
cardLayout.show(screens, "Monthly Screen");
});
GridBagConstraints a = new GridBagConstraints();
a.gridx = 0;
a.gridy = 0;
a.weightx = .5;
a.weighty = .5;
openingScreen.setFont(plainFont);
openingScreen.add(monthlyButton, a);
}