JavaFx: selectAll () method just works with keyboard focus

I use selectionAll () to select all the text in my text box, but it just works when the focus comes from the keyboard (e.g. Tab).

If I clicked in the text box, it selects the text only for a very short period of time. But it should work like with the focus that 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 - my text fields

+8
source share
2 answers

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(); } } }); } }); 
+17
source

This worked for me:

 PathField.focusedProperty().addListener((obs, wasFocused, isNowFocused) -> { if (isNowFocused) { Platform.runLater(() -> PathField.selectAll()); } }); 
0
source

All Articles