Search code examples
javaswingfontsjlabeljtextfield

Set the font of texts retrieved from JTextField


I have the user enter some words through a JTextField. I want to set the font of the string. Here is what I have so far.

Font f;
f = new Font(input.getText(), Font.ITALIC, 32);
word = new JLabel(f, SwingConstants.CENTER);

Unfortunately, Java is throwing me a compiler error because JLabel doesn't accept Font as a parameter. Is it possible to set the font of a string retrieved from a text field and have it displayed on a JFrame?


Solution

  • See JComponent.setFont(Font).

    Sets the font for this component.


    But better than a text field to set a font name, see this answer for a (styled) combo or this answer that uses a list: