As soon a TextField is focussed it selects all text. This might be nice in case that the user wants to change the entire content, but sometimes it is not desired. Additionally it breaks the select* methods if they are called within a listener on the focussed-property, if they are not called embedded inside Platform.runLater()
I appended a simple example that demonstrates the issue. The listener on the focussed property has no effect (it selects the right portion, but after that "selectAll" is called in the background).
If you press on the button it shows the right selection, because the selection is set after requesting the focus.
In short: TextField (and maybe other text components) should not select text on it's own if focused. It makes simple things complicated.
Example:
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class TextFieldTest extends Application {
@Override
public void start(Stage primaryStage) {
TextField field = new TextField();
field.setText("Some Text");
field.focusedProperty().addListener(o -> {
if (field.isFocused()) {
field.selectRange(5, field.getText().length());
}
});
Button button = new Button("Select without focus");
button.setOnAction(e -> {
field.requestFocus();
field.selectRange(5, field.getText().length());
});
VBox root = new VBox(5);
root.getChildren().addAll(field, button);
Scene scene = new Scene(root, 600, 250);
primaryStage.setTitle("Hello World!");
primaryStage.setScene(scene);
primaryStage.show();
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
}
I appended a simple example that demonstrates the issue. The listener on the focussed property has no effect (it selects the right portion, but after that "selectAll" is called in the background).
If you press on the button it shows the right selection, because the selection is set after requesting the focus.
In short: TextField (and maybe other text components) should not select text on it's own if focused. It makes simple things complicated.
Example:
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class TextFieldTest extends Application {
@Override
public void start(Stage primaryStage) {
TextField field = new TextField();
field.setText("Some Text");
field.focusedProperty().addListener(o -> {
if (field.isFocused()) {
field.selectRange(5, field.getText().length());
}
});
Button button = new Button("Select without focus");
button.setOnAction(e -> {
field.requestFocus();
field.selectRange(5, field.getText().length());
});
VBox root = new VBox(5);
root.getChildren().addAll(field, button);
Scene scene = new Scene(root, 600, 250);
primaryStage.setTitle("Hello World!");
primaryStage.setScene(scene);
primaryStage.show();
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
}