是否可以更改JavaFX的默认行为TextArea
,以便按下Tab
将焦点传递到下一个组件?
正如他指出的那样,尽管@ItachiUchiha解决方案有效,但它取决于布局(box
在他的示例中)。
基于此问题TextArea
,无论布局如何,都可以修改a的默认行为。
但是您将需要使用此私有API,该私有API可能随时更改,恕不另行通知。
在此示例中Tab
,Shitf+Tab
将具有所需的行为,同时Ctrl+Tab
将其插入"\t"
文本区域。
@Override
public void start(Stage primaryStage) {
TextArea area = new TextArea();
area.addEventFilter(KeyEvent.KEY_PRESSED, (KeyEvent event) -> {
if (event.getCode() == KeyCode.TAB) {
TextAreaSkin skin = (TextAreaSkin) area.getSkin();
if (skin.getBehavior() instanceof TextAreaBehavior) {
TextAreaBehavior behavior = (TextAreaBehavior) skin.getBehavior();
if (event.isControlDown()) {
behavior.callAction("InsertTab");
} else if (event.isShiftDown()) {
behavior.callAction("TraversePrevious");
} else {
behavior.callAction("TraverseNext");
}
event.consume();
}
}
});
VBox root = new VBox(20, new Button("Button 1"), area, new Button("Button 2"));
Scene scene = new Scene(root, 400, 300);
primaryStage.setScene(scene);
primaryStage.show();
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句