How to set the Font on Java Swing components (JTextArea, JLabel, JList)

Here are a few examples of how to set the Font on Java Swing components, including JTextArea, JLabel, and JList:

// jtextarea
textArea.setFont(new Font("Monaco", Font.PLAIN, 20));

// jlabel
label.setFont(new Font("Helvetica Neue", Font.PLAIN, 14));

// jlist
list.setFont(new Font("Helvetica Neue", Font.PLAIN, 12));

A test application

You can use code like the following Scala code to easily test different fonts. Modify however you need to, but it displays a JFrame with a JTextArea, and you can change the font on it:

package com.devdaily.sarah.gui

import java.awt._
import javax.swing._

object TextWindowBuilderTest extends App {

    val b = new TextWindowBuilder("Hello, world")
    b.setVisible(true)
    
}

/**
 * How to use:
 * 
 *   val b = new TextWindowBuilder("Hello, world")
 *   b.setVisible(true)
 *   
 *   // later
 *   b.setVisible(false)
 * 
 */
class TextWindowBuilder (
        textToDisplay: String,
        windowSize: Dimension = new Dimension(400, 300),
        location: Point = new Point(200, 200),
        textAreaRows: Int = 10,
        textAreaColumns: Int = 40,
        alpha: Float = 0.8f
        ) {

    // textarea and scrollpane
    val textArea = new JTextArea(textAreaRows, textAreaColumns)
    textArea.setFont(new Font("Helvetica Neue", Font.PLAIN, 20))
    textArea.setEditable(false)
    val scrollPane = new JScrollPane(textArea)
    textArea.setText(textToDisplay)
    scrollPane.setPreferredSize(windowSize)
    scrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS)
    scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS)
 
    // jframe
    val f = new JFrame
    f.setUndecorated(true)
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)
    f.getRootPane.putClientProperty("Window.alpha", alpha)
    f.getContentPane.add(scrollPane, BorderLayout.CENTER)
    f.setLocation(location)
    
    def setText(text: String) {
        val code = textArea.setText(text)
        invokeLater(code)
    }

    def setVisible(setVisible: Boolean) {
        if (setVisible) {
            val block = {
                f.pack
                f.setVisible(true)
            }
            invokeLater(block)
        } else {
            val block = f.setVisible(false)
            invokeLater(block)
        }
    }
    
    private def invokeLater[A](blockOfCode: => A) = {
        SwingUtilities.invokeLater(new Runnable {
            def run {
                blockOfCode
            }
        })
    }
  
  
}