Using Scala with Java Swing classes is pretty seamless

If you ever wanted to use Scala with Java Swing classes (like JFrame, JTextArea, JScrollPane, etc.), the process is pretty seamless. Here’s an example of a simple Scala/Swing application where I show a text area in a JFrame:

import java.awt.BorderLayout
import java.awt.Dimension
import javax.swing.JFrame
import javax.swing.JScrollPane
import javax.swing.JTextArea

object SwingExample extends App {

    val textArea = new JTextArea("Hello, Swing world")
    val scrollPane = new JScrollPane(textArea)

    val frame = new JFrame("Hello, Swing")
    frame.getContentPane.add(scrollPane, BorderLayout.CENTER)
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)
    frame.setSize(new Dimension(600, 400))
    frame.setLocationRelativeTo(null)
    frame.setVisible(true)

}

Technically you’ll want to display the JFrame the way I show in How to create, center, and display a Java JFrame, but this code gives you an idea of how the process works.

I’ve written much larger applications with Scala and Swing, but for today I just wanted to share some code to show that it’s a very straightforward process.