Reputation: 60
I am writing a console application and I would like to autocomplete commands when pressing tab. The problem is that when you press tab in javaFX it switches focus to another element in the application. Any way to disable this?
Upvotes: 1
Views: 2252
Reputation: 209339
Add an event filter and consume the event:
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
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 DisableFocusNavigation extends Application {
private TextField createTextField() {
TextField textField = new TextField();
textField.addEventFilter(KeyEvent.KEY_PRESSED, event -> {
if (event.getCode() == KeyCode.TAB) {
System.out.println("Tab pressed");
event.consume();
}
});
return textField ;
}
@Override
public void start(Stage primaryStage) {
TextField tf1 = createTextField();
TextField tf2 = createTextField();
VBox root = new VBox(5, tf1, tf2);
root.setAlignment(Pos.CENTER);
root.setPadding(new Insets(20));
primaryStage.setScene(new Scene(root));
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
Note that this isn't particularly good practice, as it makes it impossible to use the application without a mouse (or similar input device). You should at least check for modifier keys in the event filter, and allow for some focus traversal options.
Upvotes: 3