【发布时间】:2015-04-24 14:09:15
【问题描述】:
是否可以更改 JavaFX TextArea 的默认行为,以便按下 Tab 将焦点传递到下一个组件?
【问题讨论】:
标签: javafx-8
是否可以更改 JavaFX TextArea 的默认行为,以便按下 Tab 将焦点传递到下一个组件?
【问题讨论】:
标签: javafx-8
正如他所说,虽然@ItachiUchiha 解决方案有效,但它取决于布局(在他的示例中为box)。
基于此question,您可以修改TextArea 的默认行为,而不管布局如何。
但您需要使用此私有 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();
}
【讨论】:
skin.getBehavior()。
嗯,你当然可以这样做,但这取决于 TextArea 添加到的布局。我创建了一个简单的示例,其中TextArea 和TextField 都添加到VBox。有一个keyEventHandler 监视TextArea 上的keyPress 事件并将focus 发送给下一个孩子(如果有)
import java.util.Iterator;
import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class TextAreaTabFocus extends Application {
@Override
public void start(Stage primaryStage) throws Exception {
VBox box = new VBox();
TextArea textArea = new TextArea();
TextField textField = new TextField();
box.getChildren().addAll(textArea, textField);
final EventHandler<KeyEvent> keyEventHandler =
keyEvent -> {
if (keyEvent.getCode() == KeyCode.TAB) {
Iterator<Node> itr = box.getChildren().iterator();
while(itr.hasNext()) {
if(itr.next() == keyEvent.getSource()) {
if(itr.hasNext()){
itr.next().requestFocus();
}
//If TextArea is the last child
else {
box.getChildren().get(0).requestFocus();
}
break;
}
}
keyEvent.consume();
}
};
textArea.setOnKeyPressed(keyEventHandler);
Scene scene = new Scene(box, 200, 200);
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
【讨论】: