FormsFX Documentation

repository·master-11·Indexed 20 days ago

https://github.com/dlsc-software-consulting-gmbh/formsfx

A JavaFX framework for simplifying the creation of business application forms. FormsFX provides a fluent API for defining form semantics, automatic data binding to models, built-in validation (including Regex and Range validators), and localization support via ResourceBundleService.

Tokens
1.7K
Snippets
6
Records
9
Agent score
22%

What's inside FormsFX

  1. Bind fields to a data model

    master-11

    FormsFX is designed to manipulate data via model classes containing JavaFX properties. You can bind a field directly to a property.

    To manage data lifecycle, use:

    • persist(): Stores current field values into the bound properties.
    • reset(): Reverts field values to the values currently in the properties.

    By default, persistence is manual. You can set the BindingMode to CONTINUOUS at the form level to automate this.

    StringProperty name = new SimpleStringProperty("Hans");
    Field.ofStringType(name);
  2. How FormsFX semantics and hierarchy work

    master-11

    FormsFX uses a hierarchical semantic structure to organize form elements:

    1. Form: The largest entity, acting as the top-level container.
    2. Groups and Sections: Containers within a Form that organize related fields.
    3. Fields: The primary point of interaction where users input or view data.

    This hierarchy allows you to compose complex forms using a fluent API.

    Form loginForm = Form.of(
            Group.of(
                    Field.ofStringType(model.usernameProperty())
                            .label("Username"),
                    Field.ofStringType(model.passwordProperty())
                            .label("Password")
                            .required("This field can’t be empty")
            )
    ).title("Login");
  3. Define a form using the Fluent API

    master-11

    Forms are created using the Form.of() method, which accepts Group objects. Groups contain Field objects. You can chain methods to configure field properties like labels, requirements, and more.

    Form.of(
            Group.of(
                    Field.ofStringType("")
                            .label("Username"),
                    Field.ofStringType("")
                            .label("Password")
                            .required("This field can’t be empty")
            ),
            Group.of(…)
    ).title("Login");
  4. Localize form content

    master-11

    Methods like label() and placeholder() can accept localization keys instead of raw strings. You can provide a ResourceBundleService to the form to handle translations.

    private ResourceBundle rbEN = ResourceBundle.getBundle("demo.demo-locale", new Locale("en", "UK"));
    private ResourceBundleService rbs = new ResourceBundleService(rbEN);
    
    Form.of(…)
        .i18n(rbs);
  5. Create different types of fields

    master-11

    FormsFX provides specialized factory methods for different data types. Each type comes with a default control implementation.

    // String Control
    Field.ofStringType("CHF").label("Currency")
    
    // Integer Control
    Field.ofIntegerType(8401120).label("Population")
    
    // Double Control
    Field.ofDoubleType(41285.0).label("Area")
    
    // Boolean Control
    Field.ofBooleanType(false).label("Independent")
    
    // ComboBox Control (Single Selection)
    Field.ofSingleSelectionType(Arrays.asList("Zürich (ZH)", "Bern (BE)"), 1).label("Capital")
    
    // RadioButton Control
    Field.ofSingleSelectionType(Arrays.asList("Right", "Left"), 0)
          .label("Driving on the")
          .render(new SimpleRadioButtonControl<>())
    
    // CheckBox Control (Multi Selection)
    Field.ofMultiSelectionType(Arrays.asList("Africa", "Asia"), Collections.singletonList(2))
         .label("Continent")
         .render(new SimpleCheckBoxControl<>())
    
    // ListView Control (Multi Selection)
    Field.ofMultiSelectionType(Arrays.asList("Zürich (ZH)", "Bern (BE)"), Arrays.asList(0, 1))
         .label("Biggest Cities")
  6. Validate form fields

    master-11

    Fields are validated automatically when edited. FormsFX provides several built-in validators:

    ValidatorDescription
    CustomValidatorUses a predicate to determine validity.
    DoubleRangeValidatorValidates a number falls within a specific range.
    IntegerRangeValidatorValidates an integer falls within a specific range.
    RegexValidatorValidates text against a regular expression (e.g., email).
    SelectionLengthValidatorValidates the number of items selected in a collection.
    StringLengthValidatorValidates the length of a string.
  7. Configure Field options

    master-11

    Fields can be customized using several options to define their behavior and appearance:

    OptionDescription
    label(String)Concise description, usually visible next to the control.
    tooltip(String)Contextual hint displayed on hover or focus.
    placeholder(String)Hint displayed while the field is empty.
    required(boolean) / required(String)Determines if the field is mandatory; can include a custom error message.
    editable(boolean)Determines if the user can edit the field.
    id(String)Unique identifier for styling purposes.
    styleClass(List<String>)Adds CSS styling hooks.
    span(int) / span(ColSpan)Determines column span (1-12 or fraction).
    render(SimpleControl)Specifies a custom control for rendering the field.