Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8092126

[TextField] Add option to disable automatic selection of all text on focus

    Details

    • Type: Enhancement
    • Status: Open
    • Priority: P3
    • Resolution: Unresolved
    • Affects Version/s: 8u20, 8u31
    • Fix Version/s: tbd
    • Component/s: javafx
    • Environment:

      Any plattform

      Description

      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);
      }

      }

        Attachments

          Activity

            People

            • Assignee:
              aghaisas Ajit Ghaisas
              Reporter:
              toelgartejfx Tobias Oelgarte (Inactive)
            • Votes:
              2 Vote for this issue
              Watchers:
              5 Start watching this issue

              Dates

              • Created:
                Updated:
                Imported: