John
John

Reputation: 1711

Working Around JavaFX 2.2 Arabic Text Orientation issue

I'm using JavaFX 2.2 but everytime I typed arabic text, the order of the chracters are reversed, since the solution to this problem will only be when JavaFX 8 is released. How can I monitor the textbox and automatically reverse the characters typed back to the way it should be with something like:

arabicTextBox.setOnKeyPressed(new EventHandler<KeyEvent>() {

    @Override
    public void handle(KeyEvent t) {

        //TODO: correct arabic text order here
    }
});

A sample implementation will be appreciated.

Upvotes: 2

Views: 466

Answers (1)

Sergey Grinev
Sergey Grinev

Reputation: 34508

Use Event Filter instead of simple KeyPressed because it will allow you to consume event and override default TextField behaviour.

For simplest case (without keyboard navigation) you can handle only KeyEvent.KEY_TYPED:

final TextField tf = new TextField();
tf.addEventFilter(KeyEvent.ANY, new EventHandler<KeyEvent>() {

    @Override
    public void handle(KeyEvent t) {
        if (KeyEvent.KEY_TYPED == t.getEventType()) {
            // put character to the first position
            tf.setText(t.getCharacter() + tf.getText());
        }
        t.consume(); // doesn't allow TextField to handle keyboard events by itself
    }
});

If you want full fledged arabic-only TextField you can add logic for arrow keys, caret position, etc.

Upvotes: 1

Related Questions