Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaFx: Method selectAll() just works by focus with keyboard

i use selectionAll() to select the whole text in my textfield but it just works when the focus comes from keyboard (like Tab).

If i click with my mouse in the textfield, it selects the text just for a very short moment. But it has to work like with the focus which comes from the keyboard.

flaschenPreis.focusedProperty().addListener(new ChangeListener<Boolean>() {
    public void changed(ObservableValue ov, Boolean t, Boolean t1) {

        if ( flaschenPreis.isFocused() && !flaschenPreis.getText().isEmpty()) {
            flaschenPreis.selectAll();
        }              
    }
});


literPreis.focusedProperty().addListener(new ChangeListener() {
    public void changed(ObservableValue ov, Object t, Object t1) {

        if (literPreis.isFocused() && !literPreis.getText().isEmpty()) {
            literPreis.selectAll();
        }
    }
});

flaschenPreis und literPreis are my textfields

like image 442
Sonja Avatar asked Feb 19 '13 19:02

Sonja


2 Answers

This worked for me:

PathField.focusedProperty().addListener((obs, wasFocused, isNowFocused) -> {
    if (isNowFocused) {
        Platform.runLater(() -> PathField.selectAll());
    }
});
like image 117
Hunter Dark Avatar answered Nov 16 '22 03:11

Hunter Dark


This trick will help you :

final TextField tf = new TextField("Text");
tf.focusedProperty().addListener(new ChangeListener<Boolean>() {
    @Override
    public void changed(ObservableValue ov, Boolean t, Boolean t1) {

        Platform.runLater(new Runnable() {
            @Override
            public void run() {
                if (tf.isFocused() && !tf.getText().isEmpty()) {
                    tf.selectAll();
                }
            }
        });
    }
});
like image 31
Alexander Kirov Avatar answered Nov 16 '22 05:11

Alexander Kirov