I am very happy to announce the immediate availability of FormsFX, a framework for (surprise!) creating forms in JavaFX. FormsFX is the result of a student project at the “Fachhochschule Nordwestschweiz” (FHNW) in Switzerland. The project was initiated and sponsored by me. I believe that the result of this project is of high quality and that other projects should be able to benefit from it, hence I am contributing it as an open source project to the Java community today.
The source code and documentation can be found on GitHub at this location: https://github.com/dlemmermann/formsfx
A binary distribution (JAR file) can be downloaded from my website.
Credits
The design and the implementation were done by Sacha Schmid and Rinesch Murugathas as part of their studies for their bachelor degree.
View, Model View, Model
The form renderer of FormsFX will automatically create a form for you based on a view model. The view model defines bindings to the properties of the domain model / business model. The renderer utilises a twelve column layout (similar to bootstrap). Labels and their fields can be placed on one or more of those columns. Each field defines its own column span. If there is not enough space left for a field then it will be automatically placed on the next row. The screenshot below shows you the form created by the demo application that ships with FormsFX.
Fluent API
The fluent API of FormsFX allows you to create a view model for a form that consists of several groups and sections. A group and a section can consist of several fields. The difference between a group and a section is simply the additional title property and the fact that sections can be expanded / collapsed. The code below shows how the first three fields of the screenshot above were defined.
Form form = Form.of( Group.of( Field.ofStringType(country.nameProperty()) .label("country_label") .placeholder("country_placeholder") .required("required_error_message") .validate(StringLengthValidator.atLeast(2, "country_error_message")), Field.ofStringType(country.isoProperty()) .label("ISO_3166_label") .placeholder("ISO_3166_placeholder") .required("required_error_message") .validate(StringLengthValidator.exactly(2, "ISO_3166_error_message")), Field.ofBooleanType(country.independenceProperty()) .label("independent_label") .required("required_error_message") )).title("My Form").i18n(resourceBundle);
Fields
The following things can be defined for each field:
- the type (string, number, boolean, ….)
- a label
- a placeholder text
- whether the field is required
- one or more validators
- a tooltip
- a column span
- a format (e.g. decimal numbers)
- a renderer (normally default used, e.g. textfield for text)
- single- or multiline
- editable or not
- CSS style classes
- an ID
i18n
Any form can also be associated with a resource bundle. This allows the application to switch field labels on the fly.
So please take FormsFX for a spin and let us know what you think.
Happy coding everyone!
Leave A Comment