I wanted to create a Swing app in Ceylon and figured I'd give using the UI designer a shot. Here are the necessary steps to get it to work:
- From the menu: File -> Settings
- Navigate to Editor / GUI designer
- Make sure "Generate GUI into:" has Java source code selected
- Change "Default accessibility for UI-bound fields:" is something else than "private", for example package-private
- Click OK to save
- From the menu: File -> New -> Project
- Choose Ceylon from the left pane
- Fast-forward the wizard to the last page and choose a project name
- Click Finish
- Right-click the "source" folder -> New -> Ceylon module
- Name it "test"
- Open the "module.ceylon" file in the
source/test
folder - Replace the contents with the same-named file in this gist
- Right-click the
source/test
folder -> New -> GUI form - Type a name for the form: "TestForm"
- Let "Create a bound class" be selected
- Click OK
- For example drag a JButton to the middle of the form area
- Make sure the root JPanel has its "field name" set to "panel1"
- From the menu: Build -> Make Project
- Project should have no errors
- Right-click the
source/test
folder -> New -> Ceylon file/declaration - Name: "Test"
- Replace the contents with the same-named file in this gist
- Replace the contents with the same-named file in this gist
- Right-click the "run" of the
run()
method and choose "Run 'test.run'"
IntelliJ IDEA creates a TestForm.form file with the form specification. This is only used by IntelliJ. It also creates and updates TestForm.java which contains a TestForm class that sets up the whole form. Normally in a Java project one would write the form code directly into TestForm.java, but we want to do it in Ceylon.. So we create a Test.ceylon and there create a second class Test
which extends the generated TestForm
. Thanks to changing the accessibility setting to "package-private" our Test
ceylon subclass can see the fields of the TestForm
java class as normal. So then we can just use the Test
ceylon class in place of TestForm
.
The generated TestForm.java typically depends on some intellij classes, which normally get added to the classpath of the java project, but in ceylon we need to add a dependency to the forms_rt
maven module. One line in module.ceylon does it.
Normally also the intellij compiler produces the gui-creating code directly into the bytecode of the TestForm class. Those class files do not get used by ceylon though, Ceylon compiles the class files independently I am told, so the bytecode gets lost. That's why we needed to configure the designer to output the code into the java code instead, so ceylon sees it.
Created an example project on GitHub: https://github.com/xkr47/ceylon-swing-with-intellij-ui-designer