Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaFX: How to change the focus traversal policy?

Is it possible in JavaFX to change the focus traversal policy, like in AWT?

Because the traversal order for two of my HBoxes is wrong.

like image 891
Sonja Avatar asked Mar 06 '13 04:03

Sonja


People also ask

How do I know if a focus traversal policy is set?

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.

How do I use a custom focustraversalpolicy?

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.

What are the default focus keys for a forward traversal?

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.

How to use traversal engine strategy in JFX?

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.


2 Answers

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" /> 
like image 173
Bluehair Avatar answered Oct 13 '22 13:10

Bluehair


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

like image 24
none_ Avatar answered Oct 13 '22 13:10

none_