JavaFX is intended to replace Swing as the standard GUI library for Java SE, it supports desktop computers and web browsers. Oracle: JavaFX overview
JavaFX delivers many options for developers to decide how to implement their GUI.
Java Class: all can be done in the standard Java Class.
- FXML: The FXML scripting language can be used to separate the user interface and the back-end logic. UI can be presented
in FXML and write application logic in Java code.
- JavaFX scene builder: Can be used to define your UI.
- CSS: Cascading Style Sheets can be used to separate appearance and style.
Even all can be done in Java code, but there are many advantages for using FXML and CSS separately.
- Developer who has web design background would like to do it this way.
- It is more structured, easy to organize, divide jobs if you are working with others.
- Increase code reusability, since logic code, UI, and style are organized separately, all part can be easily used again.
- Easy to maintain, modifier, replace and debug …
###Use JavaFX to build a Login screen
Example code are created with IntelliJ, but any IDE should work the same way.
Build a JavaFX Application.
Use JavaFX scene builder to build UI.
- Reference your CSS style file in Scene Builder or add
stylesheets="@yourstylesheetName.css" to your fxml code.
- Reference your CSS styleClass in Scene Builder or add
styleClass="yourstyleClassName" to the desired Object.
This is how my FXML look like:
Logical code in Controller:
Finally define the application style in CSS:
Here is a screenshot of the Light blue theme has been built:
Source code: on GitHub
comments powered by