Taking on GUI Builders with the Swing JavaBuilder
Upon running the app, we should see:
Notice that the layout is created, all controls are aligned by baseline (thanks to the wonders of MigLayout) and even the Save and Cancel buttons are the same size (all of this possible to our MigLayout DSL). Also, thanks to databinding the Person's first and last name have been progagated from the Java class to the UI automatically.
Let's enter an invalid email address and press Save:
The integrated validation checked the input against the email validator in Apache Commons Validators and automatically displayed an error msg to the user.
Let's enter a valid email:
and press Save. The Save button was wired to execute the save() method on the Java side (via the "onAction=save" section in YAML), which in turn was flagged as a method that should be run in the background using Swing Worker (with an integrated progress bar and a Cancel option, if required):
After successfully saving, the done() method is invoked that displays the person's data. Please note that the email address we entered in the UI was automatically propagated to the Person bean via data binding:
We can now press Cancel. Since the YAML file requested a confirmation using the global "$confirm" commmand before executing the cancel method:
- JButton(name=cancel, text=button.cancel, onAction=($confirm,cancel))
we get a standard confirmation prompt:
In total we have a perfectly working typical business application with integrated control creation / layout management / data binding / Swing Worker support / input validation in less than 25 lines of YAML and with a Java class where most of the logic is purely business-related and has little, if any, UI-specific code in it.
I hope this little example will get you interested in the Swing JavaBuilder. For fans of other GUI toolkits, we have some early dev builds of the SWT JavaBuilder and some work is being done on the GTK+ JavaBuilder (which will use the Java-GNOME bindings to allow building of native Linux apps with Java).
Cheers and happy (productive) building!
P.S. You can view the source files for this tutorial app in our GitHub repository:
(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)