Here’s a little Kotlin example that uses Java Swing, including a JFrame, JScrollPane, and JTextArea:
Just added a few more lessons to “Hello, Scala” including tuples, Scala + Swing, and a complete little OOP example. Also started a Github repo for it.
I’m working on a little app for my Scala & functional programming book I currently call “Future Board.” It works a little like Flipboard in getting news headlines from different sources, but it uses Scala Futures and a few other functional programming techniques.
I recently learned how to use the Java javapackager command to build a macOS application bundle — i.e., a regular macOS application — from a Java application. In this tutorial I’ll show how to create a Mac application bundle from a simple Java class, in this case a Java Swing class.
Whenever you need to understand the difference between the Java JTextArea, JTextPane, and JEditorPane, check out this Sun/Oracle link.
I haven’t tested this with other Java components, but if you want/need to get the “system font”, this code gets the default system font from a
val outputArea = new JEditorPane val fontFamily = outputArea.getFont.getFamily
That code is written in Scala, but as you can see, it converts easily to Java. On Mac OS X 10.10,
fontFamily ends up being “Lucida Grande”.
If you ever need to add a keystroke to a Java Swing application (or Scala Swing), this code may help. It shows how to add the [Command][M] keystroke on a Mac OS X system to a Swing application. It makes the keystroke available in a “Window” menu:
If you ever need to add an image to a Scala/Java Swing application, I hope this example will help. It shows how to create an
getResource, then add that image to a
JLabel, then add that label to a
If you ever wanted to get access to global operating system keystrokes from a Java or Scala Swing application, you can do it with the jnativehook library. Here’s a short demo:
Here’s the full Scala source code for the demo. I put a few comments in the code to highlight the important areas:
Is Swing dead? I don’t know. I’ve written several Swing apps that I use every day, but I can’t speak for the rest of the world.
What I can say is that I really like Scala Swing. How I wish it was available ten years ago ... you’ll never know.
But getting to the point, after working with Scala Swing a little bit, I decided to see if I could write a web browser with it. The short answer is that here’s what the browser looks like: