This question already has an answer here:
这个问题在这里已有答案:
- JavaFX: How to change the focus traversal policy? 9 answers
JavaFX:如何更改焦点遍历策略? 9个答案
Before asking, i should say that the answer of can be found maybe in JavaFX: How to change the focus traversal policy?, but i don't looking for any java code, i would like a way by editing the Fxml file
在询问之前,我应该说可以在JavaFX中找到答案:如何更改焦点遍历策略?,但我不寻找任何java代码,我想通过编辑Fxml文件的方式
There is a FXML file that has some TextFields in it and i would like to change focus traversal policy of my TextField : I mean, At the beginning of the application the cursor must be located in id1 and then by pressing TAB button it must go to id2 and etc.
有一个FXML文件,里面有一些TextFields,我想改变我的TextField的焦点遍历策略:我的意思是,在应用程序的开头,光标必须位于id1,然后按TAB键,它必须转到id2等
!!!! ID1-->ID2-->ID3-->ID4-->ID5-->ID6 !!!!
!!!! ID1 - > ID2 - > ID3 - > ID4 - > ID5 - > ID6 !!!!
Notice that the position of my TextField should be as like as below picture.
请注意,我的TextField的位置应该如下图所示。
So for doing that i change the order of textFields in FXML File:
所以为了做到这一点,我改变了FXML文件中textFields的顺序:
<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1">
<children>
<TextField fx:id="id1" layoutX="401.0" layoutY="41.0" promptText="id1" />
<TextField fx:id="id2" layoutX="168.0" layoutY="41.0" promptText="id2" />
<TextField fx:id="id3" layoutX="401.0" layoutY="110.0" promptText="id3" />
<TextField fx:id="id4" layoutX="168.0" layoutY="110.0" promptText="id4" />
<TextField fx:id="id5" layoutX="401.0" layoutY="174.0" promptText="id5" />
<TextField fx:id="id6" layoutX="401.0" layoutY="249.0" promptText="id6" />
</children>
</AnchorPane>
it works fine for me, but now i have the big problem, when i wrap two TextFields ( Id1 and Id2 ) in HBox. this approach doesn't give me the correct result and the order of focus traversal has changed.
它适用于我,但现在我有一个大问题,当我在HBox中包装两个TextFields(Id1和Id2)。这种方法不能给我正确的结果,焦点遍历的顺序也发生了变化。
The modified FXML is like below:
修改后的FXML如下:
<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="Bank.LayoutController">
<children>
<HBox layoutX="168.0" layoutY="41.0" spacing="80.0">
<children>
<TextField fx:id="id2" layoutX="168.0" layoutY="41.0" promptText="id2" />
<TextField fx:id="id1" layoutX="401.0" layoutY="41.0" promptText="id1" />
</children>
</HBox>
<TextField fx:id="id3" layoutX="401.0" layoutY="110.0" promptText="id3" />
<TextField fx:id="id4" layoutX="168.0" layoutY="110.0" promptText="id4" />
<TextField fx:id="id5" layoutX="401.0" layoutY="174.0" promptText="id5" />
<TextField fx:id="id6" layoutX="401.0" layoutY="249.0" promptText="id6" />
</children>
</AnchorPane>
and the screen shot of result becomes like below picture ( as you can see the staring point has changed to id2 and then by pressing tab the cursor goes to id1... !!!! ID2-->ID1-->ID3-->ID4-->ID5-->ID5 !!! )
并且结果的屏幕截图变得像下面的图片(正如你可以看到凝视点已经变为id2然后按Tab键,光标变为id1 ...... !!!! ID2 - > ID1 - > ID3-- > ID4 - > ID5 - > ID5 !!!)
SO !?!? how can i change the Focus Traversal of my TextField when the user click the TAB.
所以!?!?当用户单击TAB时,如何更改TextField的焦点遍历。
It seems if you just have an anchorPane it's very easy, but if you have HBox it starts from Right side to left Side.
看起来如果你只有一个anchorPane它很容易,但是如果你有HBox它从右侧开始到左侧。
By default when the the tab key is pressed, focus shifted (right wards) to the next component but as i mentioned i want Left to Right !!
默认情况下,当按下Tab键时,焦点移动(右侧病房)到下一个组件,但正如我所说,我想要从左到右!!
Is there any solution?
有什么解决方案吗?
1 个解决方案
#1
There are multiple ways to achieve the stated behaviour. A very simple technique would be to make use of NodeOrientation on the Parent of the TextFields, along with the ordering of it in the Parent.
有多种方法可以实现所述行为。一种非常简单的技术是在TextFields的Parent上使用NodeOrientation,以及在Parent中对它进行排序。
I have re-vamped your code to make it more maintainable. The following points can be noted for better understanding:
我重新编写了代码,使其更易于维护。为了更好地理解,可以注意以下几点:
- Instead of 1, I am using 4 HBox contained in a VBox
- Each of these HBox has one / two TextFields according to the need. It may contain more than two
- The nodeOrientation for each HBox is
RIGHT_TO_LEFT
- The first element of first HBox is TextField
id1
and notid2
, which makes it the first element of be focused on when the scene is loaded - All the HBox's are contained in a VBox to make the traversal of focus between TextField's contained in different HBox's
而不是1,我使用的是VBox中包含的4个HBox
根据需要,这些HBox中的每一个都有一个/两个TextField。它可能包含两个以上
每个HBox的nodeOrientation为RIGHT_TO_LEFT
第一个HBox的第一个元素是TextField id1而不是id2,这使得它成为加载场景时要关注的第一个元素
所有HBox都包含在VBox中,以便在不同的HBox中包含的TextField之间进行焦点遍历
FXML
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<VBox xmlns="http://javafx.com/javafx/8.0.40" xmlns:fx="http://javafx.com/fxml/1">
<children>
<HBox maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" nodeOrientation="RIGHT_TO_LEFT" prefHeight="32.0" prefWidth="600.0">
<children>
<TextField promptText="id1" />
<TextField promptText="id2" />
</children>
</HBox>
<HBox maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" nodeOrientation="RIGHT_TO_LEFT" prefHeight="35.0" prefWidth="600.0">
<children>
<TextField promptText="id3" />
<TextField promptText="id4" />
</children>
</HBox>
<HBox maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" nodeOrientation="RIGHT_TO_LEFT" prefHeight="34.0" prefWidth="600.0">
<children>
<TextField promptText="id5" />
</children>
</HBox>
<HBox maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" nodeOrientation="RIGHT_TO_LEFT" prefHeight="400.0" prefWidth="600.0">
<children>
<TextField promptText="id6" />
</children>
</HBox>
</children>
</VBox>
#1
There are multiple ways to achieve the stated behaviour. A very simple technique would be to make use of NodeOrientation on the Parent of the TextFields, along with the ordering of it in the Parent.
有多种方法可以实现所述行为。一种非常简单的技术是在TextFields的Parent上使用NodeOrientation,以及在Parent中对它进行排序。
I have re-vamped your code to make it more maintainable. The following points can be noted for better understanding:
我重新编写了代码,使其更易于维护。为了更好地理解,可以注意以下几点:
- Instead of 1, I am using 4 HBox contained in a VBox
- Each of these HBox has one / two TextFields according to the need. It may contain more than two
- The nodeOrientation for each HBox is
RIGHT_TO_LEFT
- The first element of first HBox is TextField
id1
and notid2
, which makes it the first element of be focused on when the scene is loaded - All the HBox's are contained in a VBox to make the traversal of focus between TextField's contained in different HBox's
而不是1,我使用的是VBox中包含的4个HBox
根据需要,这些HBox中的每一个都有一个/两个TextField。它可能包含两个以上
每个HBox的nodeOrientation为RIGHT_TO_LEFT
第一个HBox的第一个元素是TextField id1而不是id2,这使得它成为加载场景时要关注的第一个元素
所有HBox都包含在VBox中,以便在不同的HBox中包含的TextField之间进行焦点遍历
FXML
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<VBox xmlns="http://javafx.com/javafx/8.0.40" xmlns:fx="http://javafx.com/fxml/1">
<children>
<HBox maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" nodeOrientation="RIGHT_TO_LEFT" prefHeight="32.0" prefWidth="600.0">
<children>
<TextField promptText="id1" />
<TextField promptText="id2" />
</children>
</HBox>
<HBox maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" nodeOrientation="RIGHT_TO_LEFT" prefHeight="35.0" prefWidth="600.0">
<children>
<TextField promptText="id3" />
<TextField promptText="id4" />
</children>
</HBox>
<HBox maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" nodeOrientation="RIGHT_TO_LEFT" prefHeight="34.0" prefWidth="600.0">
<children>
<TextField promptText="id5" />
</children>
</HBox>
<HBox maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" nodeOrientation="RIGHT_TO_LEFT" prefHeight="400.0" prefWidth="600.0">
<children>
<TextField promptText="id6" />
</children>
</HBox>
</children>
</VBox>