pranahata
pranahata

Reputation: 540

JavaFX 8 TextArea loose focus on tab

Is it possible to change the default behaviour of a JavaFX TextArea, so that pressing Tab passes the focus to the next component?

Upvotes: 3

Views: 1719

Answers (2)

ItachiUchiha
ItachiUchiha

Reputation: 36792

Well, you definitely can do this, but it depends on the Layout to which the TextArea is added to. I have created a simple example where a TextArea and a TextField are both added to a VBox. There is a keyEventHandler which monitors the keyPress event on the TextArea and sends the focus to the next child(if any)

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);
    }
}

Upvotes: 1

Jos&#233; Pereda
Jos&#233; Pereda

Reputation: 45486

While @ItachiUchiha solution works, as he states, it depends on the layout (box in his sample).

Based on this question, you can modify the default behavior of a TextArea, regardless of the layout.

But you will need to use for this private API, which may change at any time without notice.

In this sample Tab and Shitf+Tab will have the desired behavior, while Ctrl+Tab will insert "\t" on the text area.

@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();
}

Upvotes: 5

Related Questions