Here’s a little Kotlin example that uses Java Swing, including a JFrame, JScrollPane, and JTextArea:
In two small tests where GraalVM was able to create a native executable, the native executable ran significantly faster than the equivalent Scala/Java code running with the Java 8 JVM, and also reduced RAM consumption by a whopping 98% in a long-running example. On the negative side, GraalVM currently doesn’t seem to work with Swing applications.
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: