Select content from a TextField by winning Focus

1

I want that when a control TextField wins the focus its content is selected. At the moment only the cursor appears where I click with the mouse and the selection disappears. I've tried with this code:

miTextField.focusedProperty().addListener((ObservableValue<? extends Boolean> 
    observable, Boolean valorAnterior, Boolean valorActual) -> {
    if (valorActual){   
         miTextField.selectAll();        
    }        
});

Substituting miTextField.selectAll() for miTextField.selectRange(0, miTextField.getText().length()) does not work either.

    
asked by 12.09.2016 в 21:00
source

1 answer

2

selectAll() should work , however you have to use Plaform.Runlater () to work as you wish:

    miTextField.focusedProperty().addListener(new ChangeListener<Boolean>()
    {
        @Override
        public void changed(ObservableValue<? extends Boolean> observable, Boolean valorAnterior, Boolean valorActual)
        {

Platform.runLater(new Runnable() {
                @Override
                public void run() {
                    if (miTextField.isFocused() && !miTextField.getText().isEmpty()) {
                        miTextField.selectAll();
                    }
                }
            });


        }
    });
    
answered by 12.09.2016 / 21:32
source