DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
View Events Video Library
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Integrating PostgreSQL Databases with ANF: Join this workshop to learn how to create a PostgreSQL server using Instaclustr’s managed service

Mobile Database Essentials: Assess data needs, storage requirements, and more when leveraging databases for cloud and edge applications.

Monitoring and Observability for LLMs: Datadog and Google Cloud discuss how to achieve optimal AI model performance.

Automated Testing: The latest on architecture, TDD, and the benefits of AI and low-code tools.

Related

  • JavaFX Gets Video Capabilities
  • The Challenges of a JavaFX Reboot
  • Calling JavaFX From Java?
  • Wrapping Mule4 — Secure Properties with TornadoFx

Trending

  • Analyzing Stock Tick Data in SingleStoreDB Using LangChain and OpenAI's Whisper
  • Best Practices for Developing Cloud Applications
  • Extracting Maximum Value From Logs
  • A Better Web3 Experience: Account Abstraction From Flow (Part 1)
  1. DZone
  2. Coding
  3. Java
  4. Validation in JavaFX

Validation in JavaFX

What I really wanted to do was set a breakpoint and examine the state of the objects at runtime

Pedro Duque Vieira user avatar by
Pedro Duque Vieira
·
Aug. 07, 14 · Tutorial
Like (1)
Save
Tweet
Share
35.51K Views

Join the DZone community and get the full member experience.

Join For Free

validation is one thing that’s missing from the core javafx framework. to fill in this gap there is already a 3rd party validation library that’s present in controlsfx . however there’s one issue i have with it: it wasn’t created with fxml in mind. that’s not to say it isn’t a good library, it just misses this detail and for me this is a no go. because of that i decided to create my own validation framework: fxvalidation .

how it works

to show you how fxvalidation works let’s start from the bottom up, by showing you an example of what an fxml file might look like when using this library. this is a simple example of a login screen where the user needs to enter both an user name and a password:

<label>
  <text>user name:</text>
</label>
<textfield fx:id="username" id="username"></textfield>
<label>
  <text>password:</text>
</label>
<passwordfield fx:id="password" id="password"></passwordfield>

<button text="submit" onaction="#submitpressed"></button>

<fx:define>
  <requiredfield fx:id="requiredfield1" >
    <srccontrol>
      <fx:reference source="username"></fx:reference>
    </srccontrol>
  </requiredfield>
  <requiredfield fx:id="requiredfield2" >
    <srccontrol>
      <fx:reference source="password"></fx:reference>
    </srccontrol>
  </requiredfield>
</fx:define>

<errorlabel message="please enter your username">
  <validator>
    <fx:reference source="requiredfield1"></fx:reference>
  </validator>
</errorlabel>
<errorlabel message="please enter your password">
  <validator>
    <fx:reference source="requiredfield2"></fx:reference>
  </validator>
</errorlabel> 

on the beginning of the fxml snippet i define a textfield and password field for entering the login details. other than that there’s also a submit button so the user may send the login information to the system. after that comes the interesting part. first we define a couple of validators of type requiredfield. this validators, check whether the input in question is empty and if so they store that the validation has errors in a flag. there’s also other types of validators built-in the fxvalidation framework but we’ll get to that in a bit. finally we define a couple of errorlabels. this are nodes that implement ivalidationdisplay, any class that implements this interface is a class whose purpose is to display information to the user whenever there is an error in the validation process. currently there is only one of this classes in the framework: the errorlabel. finally we need to call validation when the user clicks the submit button, this is done in the controller on the submit method:

public void submitpressed(actionevent actionevent) {
  requiredfield1.eval();
  requiredfield2.eval();
}

this will trigger validation for the validators that we have defined. if there are errors the errorlabels will display the error message that was defined in them. there’s also one extra thing the validators do: they add in the css style class of “error” to every control that is in error after the validation process has taken effect. this allows the programmer to style the controls differently using css whenever this controls have the error class appended to them. the programmer can check for errors in the validation process by checking the property haserrors in the validators.

and here’s our example in action:

validation

the details

from what i’ve shown you above we can see that there are basically 2 types of classes involved:

  • the validator: takes care of checking if the target control ( srccontrol ) conforms to the validation rule. if not it appends the “error” style class to target control sets its haserrors property to true. all validators extend from validatorbase .
  • the error display information: this takes care of informing the user what went wrong with the validation, it might be that the field is required, the fields content doesn’t have the necessary number of characters, etc. all this classes implement ivalidationdisplay .

in the library there are currenctly 3 validators and only one error “displayer” which is errorlabel. the validators are the following:

  • requiredfield: checks whether the target control ( srccontrol ) has content, if it doesn’t it gives an error.
  • cardinalityvalidator: checks whether the target control ( srccontrol ) has at least a min number of characters and a maximum of max number of characters.
  • regexvalidator: checks the content of the target control ( srccontrol ) against a given regular expression

and that’s it.

i’d love to collaborate with controlsfx or if someone else wants to contribute fell free to do so. so that we can achieve the definitive validation framework.

JavaFX

Published at DZone with permission of Pedro Duque Vieira, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • JavaFX Gets Video Capabilities
  • The Challenges of a JavaFX Reboot
  • Calling JavaFX From Java?
  • Wrapping Mule4 — Secure Properties with TornadoFx

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends: