Scala/Java - How to change the font on a TitledBorder on a JPanel

As a quick note, in a Scala/Swing application, I needed to change the font on a TitledBorder on two JPanel instances, and this code worked:

// the desired font
val font = new Font("Helvetica Neue", Font.PLAIN, 15)

// set the font on the titledborder instances
volumeControlPanel.getBorder.asInstanceOf[TitledBorder].setTitleFont(font)
keysPanel.getBorder.asInstanceOf[TitledBorder].setTitleFont(font)

The key in the solution is to get the Border from the JPanel, then cast it to a TitledBorder, where you can set the font. (Of course this assumes that you’re trying to change the font on a TitledBorder component.) While the example shown here is written in Scala, it can easily be converted to Java.