Is it possible in JavaFX to change the focus traversal policy, like in AWT?
Because the traversal order for two of my HBox
es is wrong.
Sets or gets the focus traversal policy or determines if a policy has been set. Note that setting a focus traversal policy on a container that is not the focus cycle root may have no apparent effect. A value of null means that a policy has not been explicitly set. If no policy has been set, a policy is inherited from the parent focus cycle root.
To use a custom FocusTraversalPolicy, implement the following code on any focus cycle root. MyOwnFocusTraversalPolicy newPolicy = new MyOwnFocusTraversalPolicy (); frame.setFocusTraversalPolicy (newPolicy); You can remove the custom focus traversal policy by setting the FocusTraversalPolicy to null, which will restore the default policy.
The set of default focus keys for a forward traversal. For multi-line text components, these keys default to Control-Tab. For all other components, these keys default to Tab and Control-Tab. The set of default focus keys for a backward traversal. For multi-line text components these keys default to Control-Shift-Tab.
There is a back door in JFX about traversal engine strategy substitution : you can subclass the internal class com.sun.javafx.scene.traversal.TraversalEngine call to apply that engine. You can observe the code of OpenJFX, to understand, how it works, and what you can do.
The simplest solution is to edit the FXML file and reorder the containers appropriately. As an example, my current application has a registration dialog in which a serial number can be entered. There are 5 text fields for this purpose. For the focus to pass from one text field to the other correctly, I had to list them in this way:
<TextField fx:id="tfSerial1" layoutX="180.0" layoutY="166.0" prefWidth="55.0" /> <TextField fx:id="tfSerial2" layoutX="257.0" layoutY="166.0" prefWidth="55.0" /> <TextField fx:id="tfSerial3" layoutX="335.0" layoutY="166.0" prefWidth="55.0" /> <TextField fx:id="tfSerial4" layoutX="412.0" layoutY="166.0" prefWidth="55.0" /> <TextField fx:id="tfSerial5" layoutX="488.0" layoutY="166.0" prefWidth="55.0" />
Bluehair's answer is right, but you can do this even in JavaFX Scene Builder.
You have Hierarchy panel in left column. There are all your components from scene. Their order represents focus traversal order and it responds to their order in FXML file.
I found this tip on this webpage:www.wobblycogs.co.uk
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With