JavaFX TextArea: how to set tabulation width

Finally I found a way to do this.

It seems that the setOnKeyPressed() method is not good for this task because the event is handled after the keyPress action is executed.

The addEventFilter() handles the events before their actions are executed, so you can manipulate the events.

My new code:

taInput.addEventFilter(KeyEvent.KEY_PRESSED, new EventHandler<KeyEvent>() {
    @Override
    public void handle(KeyEvent e) {
        if (e.getCode() == KeyCode.TAB) {
            String s = StringUtils.repeat(' ', config.getTabSpacesCount());
            taInput.insertText(taInput.getCaretPosition(), s);
            e.consume();
        }
    }
});