Swing GUI application fails to compile in Fire

Firstly, I’m not sure if this is the right place for generic questions about the Java target. Sorry if this is inconvenient.

I’m trying to build a simple Swing GUI project using Elements, this is the code (converted to Swift from a Java tutorial):

import javax.swing.JFrame
import javax.swing.JButton

let f = JFrame()

let b = JButton("click")
b.setBounds(130, 100, 100, 40)
f.add(b)

f.setSize(400, 500)
f.setLayout(nil)
f.setVisible(true)

The compile fails with this error:

E: Unknown namespace "javax.swing.JFrame" in uses list [/Volumes/Data/dev/elements/swingtest/Program.swift (1)]
E: Unknown namespace "javax.swing.JButton" in uses list [/Volumes/Data/dev/elements/swingtest/Program.swift (2)]
E: Unknown identifier "JFrame" [/Volumes/Data/dev/elements/swingtest/Program.swift (4)]
E: Unknown identifier "JButton" [/Volumes/Data/dev/elements/swingtest/Program.swift (6)]
   Project 'swingtest' failed to build.
   Solution 'swingtest' failed to build project 'swingtest'.

This seems strange, because I’ve tested this with standard Java in IntelliJ and it works fine, using the same JDK. Why is it different in Elements?

I’ve discovered that import is actually bringing in namespaces and can’t be used for specific classes or functions, but even after changing the first two lines to be import javax.swing, it’s still an unknown namespace.

I’m not familiar with the Swing libraries, but this sounds - assuming the type names and namespaces are correct - like you are missing a reference?

I got it. Swing is built into the JDK, but I made the mistake of thinking all JDK classes were referenced by default. Adding the java.desktop reference fixes the compile error.

1 Like

Excellent. happy to hear.