JavaFX is a Java-based UI platform for developing desktop applications on a variety of platforms. It is pitched as a replacement for Swing.
JavaFX UIs can be described using FXML files (.fxml). FXML files are a special type of XML file for describing the UI in a similar way to HTML. This style of designing a UI is called a declarative style. The alternative method to create JavaFX UIs is to use a procedural style, using normal Java code.
The FXML method is great for describing static (or semi-static) JavaFX UI elements. That is, all of the UI elements which don’t change (or only partially change) at run time. FXML is not good for creating the UI elements which are dynamically created at runtime.
Both design methodologies can be implemented in the same project.
JavaFX sports a number of different layout controls to arrange your UI with. These include (sorted in alphabetical order):
|AnchorPane||Allows child controls to be anchored to the top, bottom, left, right or center of the AnchorPane.|
|BorderPane||Allows child controls to be placed and the top, bottom, left, right or center of the BorderPane.|
|FlowPane||Arranges all of it's child controls either horizontally or vertically and then wraps once it reaches a certain size limit. Like a TilePane except each child control gets allocated a different amount of space.|
|GridPane||Arranges it's child controls in a grid (aka table).|
|HBox||Arranges it's child controls horizontally.|
|StackPane||Stacks all of it's child controls on-top of each other.|
|TilePane||Arranges all of it's child controls either horizontally or vertically and then wraps once it reaches a certain size limit. Like a FlowPane except each child control gets allocated the same amount of space.|
|VBox||Arranges it's child controls vertically.|
One thing that annoys me is that there is no row or column sub-controls (like
Circular buttons can be easily made by modifying the -fx-background-radius property.
Attaching To Button Pressed And Released Events
Sometimes, just responding to an “on click” event is not enough, and you need to be able to determine when the user both pressed and released the mouse (or finger) on the button. Handlers for MOUSE_PRESSED and MOUSE_RELEASED events can be attached to JavaFX buttons using the following code:
Double clicks on JavaFX nodes can be found with the following code (using Java 8 lambda notation):
Note that it gets slightly more difficult if you need to detect single-clicks also.
Adding Properties To Custom Controls
Properties can be added to custom controls by providing a getter and a setter. Once this has been done, they can be set in a .fxml file in the same manner as any other property:
This work is licensed under a Creative Commons Attribution 4.0 International License .