我有一个简单的TableView,具有从右到左的节点方向和setCellSelectionEnabled(true)。现在,当在表格上按下向右箭头键时,可遍历的焦点将作用于另一侧,因此将选择左侧的单元格。另一面也是如此。当按下左箭头键时,将选择右侧的单元格。出于区域目的,TableView的节点方向属性必须保留RTL。那我该如何解决这个问题呢?
这是TableView FXML代码:
<TableView fx:id="table" editable="true" layoutX="97.0" layoutY="170.0" nodeOrientation="RIGHT_TO_LEFT" prefHeight="400.0" prefWidth="816.0" AnchorPane.bottomAnchor="25.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="121.0">
<placeholder>
<Label text="داده ای یافت نشد :(" />
</placeholder>
<columns>
<TableColumn fx:id="numberColumn" maxWidth="120.0" minWidth="80.0" text="ردیف" />
<TableColumn fx:id="nameColumn" maxWidth="400.0" minWidth="300.0" prefWidth="300.0" text="نام ماده اولیه" />
<TableColumn fx:id="categoryColumn" maxWidth="300.0" minWidth="150.0" prefWidth="150.0" text="دسته بندی" />
<TableColumn fx:id="priceColumn" maxWidth="300.0" minWidth="220.0" prefWidth="220.0" text="قیمت هر کیلوگرم(ریال)" />
<TableColumn fx:id="unitColumn" maxWidth="200.0" minWidth="100.0" prefWidth="100.0" text="واحد" />
<TableColumn fx:id="numberOfUsesColumn" maxWidth="120.0" minWidth="120.0" prefWidth="120.0" text="دفعات استفاده" />
<TableColumn fx:id="deleteRowColumn" maxWidth="90.0" minWidth="90.0" prefWidth="90.0" resizable="false" text="حذف سطر" />
</columns>
<columnResizePolicy>
<TableView fx:constant="CONSTRAINED_RESIZE_POLICY" />
</columnResizePolicy>
</TableView>
答案 0 :(得分:1)
在表导航中不遵守RTL是bug that's fixed in openjfx15。
在此之前,我们可以四处乱逛(假设是否允许我们变脏,那就是使用反射来访问TableViewSkin中的私有字段,使用内部api和实现细节;)
在代码中,类似(此处仅用于左/右,对于带有修饰符的映射,需要做同样的事情):
protected void hackNavigation(TableView<?> table) {
TableViewSkin<?> skin = (TableViewSkin<?>) table.getSkin();
// access private field reflectively
// use your own favorite utility method :)
TableViewBehavior<?> behavior = (TableViewBehavior<?>)
FXUtils.invokeGetFieldValue(TableViewSkin.class, skin, "behavior");
// access mappings
ObservableList<Mapping<?>> mappings = behavior.getInputMap().getMappings();
// lookup the original mappings for left/right
KeyBinding leftBinding = new KeyBinding(KeyCode.LEFT);
KeyBinding rightBinding = new KeyBinding(KeyCode.RIGHT);
KeyMapping leftMapping = getMapping(mappings, leftBinding);
KeyMapping rightMapping = getMapping(mappings, rightBinding);
// remove the original mappings
mappings.removeAll(leftMapping, rightMapping);
// create new mappings with the opposite event handlers and add them
KeyMapping replaceRight = new KeyMapping(rightBinding, leftMapping.getEventHandler());
KeyMapping replaceLeft = new KeyMapping(leftBinding, rightMapping.getEventHandler());
mappings.addAll(replaceRight, replaceLeft);
}
/**
* Utility method to get hold of a KeyMapping for a binding.
* Note: only use if certain that it is contained, or guard against failure
*/
protected KeyMapping getMapping(ObservableList<Mapping<?>> mappings, KeyBinding keyBinding) {
Optional<KeyMapping> opt = mappings.stream()
.filter(mapping -> mapping instanceof KeyMapping)
.map(mapping -> (KeyMapping) mapping)
.filter(keyMapping -> keyMapping.getMappingKey().equals(keyBinding))
.findAny()
;
return opt.get();
}
// useage
stage.show();
hackNavigation(myTable);