I’m experimenting with writing some Swift code to target the JVM with Swing APIs. I’m working with Fire on macOS. Bit of background: I’m an experienced Java developer (written large Swing-based apps) and iOS/macOS developer (Swift; I have reasonably successful apps on the app store).
Now, I’m trying to get the simplest of simple Swing apps up and running - a JFrame with a JLabel. First of all, the project template I’ve had the most success with is the console application, and then modifying from there. This is what I’ve come up with:
import javax.swing
import java.awt
System.out.println("The magic happens here.")
EventQueue.invokeAndWait {
System.out.println("Hello from the event queue")
let frame = JFrame("This is the frame title")
let label = JLabel("This is a label")
frame.getContentPane().setLayout(FlowLayout())
frame.getContentPane().add(label)
frame.pack()
frame.setVisible(true)
}
System.`in`.read()
If I run this from within Fire, it fails to load any of the Swing-related UI classes or resources, although it does spit out the message “Hello from the event queue”. I have cooper, rt and swift as References in the project. The last line (System.in.read()
) seems necessary because even though the event thread is running, when execution reaches the end of the file, the program terminates (unlike the Java language equivalent, compiled with javac).
If I run the compiled jar output from the above program, then it runs fine - I get the frame with label, as expected, with no runtime errors. However, this is quite tedious - write code in Fire, build, then switch to terminal in order to run/view the results. What am I missing in terms of configuring the project so that I can just run it from the IDE?
For reference, here is the roughly equivalent Java code:
import java.util.*;
import javax.swing.*;
import java.awt.*;
public class Main implements Runnable {
public static void main(String[] args) throws Exception {
System.out.println("The magic happens here.");
EventQueue.invokeAndWait(new Main());
}
public void run() {
System.out.println("Hello from the event queue");
JFrame frame = new JFrame("This is the frame title");
JLabel label = new JLabel("This is a label");
frame.getContentPane().setLayout(new FlowLayout());
frame.getContentPane().add(label);
frame.pack();
frame.setVisible(true);
}
}
One more thing - am I missing something in the IDE? Code completion lets me complete function/class names, but it provides no hints whatsoever about function parameters that I can see, unless you first type the () after the method call, then cursor back into the brackets, then press command-P, which is quite tedious. Then it only tells you the types of the arguments in the java library, and not the parameter names.
Finally, the editor could use a few “standard” keyboard shortcuts for text editing that work in all standard Apple text components e.g. OPTION+DEL should delete the previous word. CMD+DEL should delete to beginning of line, etc.