Question

In JavaFX8 I want to know how it's possible to don't add a tabulation in a TextArea when you press tab and go the the next field like TextField do.

In JavaFX2 I resolve this problem with the following code

public class TabTextAreaSkin extends TextAreaSkin implements Skin<TextArea> {

public TabTextAreaSkin(final TextArea textArea) {
    super(textArea);
    textArea.focusedProperty().addListener(new ChangeListener<Boolean>() {
        @Override
        public void changed(ObservableValue<? extends Boolean> observableValue, Boolean aBoolean, Boolean aBoolean2) {
            if (!aBoolean2) {
                textArea.deselect();
            }
        }
    });
    textArea.addEventFilter(KeyEvent.KEY_PRESSED, new EventHandler<KeyEvent>() {
        @Override
        public void handle(KeyEvent event) {
            if (event.getCode() == KeyCode.TAB) {
                SkinBase skin = (SkinBase) textArea.getSkin();
                if (skin.getBehavior() instanceof TextAreaBehavior) {
                    TextAreaBehavior behavior = (TextAreaBehavior) skin.getBehavior();
                    if (event.isShiftDown()) {
                        behavior.callAction("InsertTab");
                    } else {
                        behavior.callAction("TraverseNext");
                    }
                    event.consume();
                }
            }
        }
    });
}

}

But in JavaFX8 there is no getBehavior() method on the SkinBase. So I don't know what to do. Someone get the solution ?

Was it helpful?

Solution 2

If someone want the solution

public class TabTextArea extends TextArea {
/**
 * The famous {@link org.slf4j.Logger}
 */
private static final Logger logger = LoggerFactory.getLogger(TabTextArea.class);

public TabTextArea() {
    this.addEventFilter(KeyEvent.KEY_PRESSED, new EventHandler<KeyEvent>() {
        @Override
        public void handle(KeyEvent event) {
            if (event.getCode() == KeyCode.TAB) {
                TextAreaSkin skin = (TextAreaSkin) getSkin();
                if (skin.getBehavior() instanceof TextAreaBehavior) {
                    TextAreaBehavior behavior = (TextAreaBehavior) skin.getBehavior();
                    if (event.isControlDown()) {
                        behavior.callAction("InsertTab");
                    } else {
                        behavior.callAction("TraverseNext");
                    }
                    event.consume();
                }
            }
        }
    });
}

}

OTHER TIPS

Try to use the com.sun.javafx.scene.traversal.TraversalEngine. Be aware of this is for internal use only.

final TextArea area = new TextArea();
final TraversalEngine engine = new TraversalEngine(root, false);
final KeyCombination kc = new KeyCodeCombination(KeyCode.TAB);

area.addEventFilter(KeyEvent.KEY_PRESSED, new EventHandler<KeyEvent>() {

    @Override
    public void handle(KeyEvent event) {
        if (kc.match(event)) {
            engine.trav(area, Direction.NEXT);
            event.consume();
        }
    }
});

The other cumbersome but safe way can be:

area.addEventFilter(KeyEvent.KEY_PRESSED, new EventHandler<KeyEvent>() {

    @Override
    public void handle(KeyEvent event) {
        if (kc.match(event)) {
            int ind = vBox.getChildren().indexOf(area);
            Node next = vBox.getChildren().get(ind + 1);
            next.requestFocus();
            event.consume();
        }
    }
});
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top