Clear prompt text in JavaFX TextField only when user starts typing

前端 未结 5 683
天涯浪人
天涯浪人 2020-12-01 06:16

The default behaviour is that the prompt text in the field is erased as the field is being focused. That is when the marker is in the field.

Is it possible to confi

5条回答
  •  再見小時候
    2020-12-01 06:20

    Assuming you're setting prompt text via:

        @FXML TextField tf;
    
        public void someMethod() {
            tf.setPromptText("This is the prompt text");
        }
    

    You can instead use tf.setText(String str) to set initial text.

    Then, in the initialize method of your controller, add:

        tf.setOnKeyTyped(new EventHandler() {
    
            @Override
            public void handle(KeyEvent event) {
                tf.setText(null);
    
            }
        });
    

    A link that you can refer to for additional help: Click me!

提交回复
热议问题