单击JavaFX时突出显示TextField中的所有文本 [英] Highlight all text in TextField upon click JavaFX

查看:52
本文介绍了单击JavaFX时突出显示TextField中的所有文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作JavaFX应用程序,启动后,TextField的默认文本为请输入此值",用户应删除该文本并输入一个值.但是,每次单击,拖动并突出显示所有文本都是很痛苦的.有没有办法做到这一点,所以当用户单击TextField时,它会自动突出显示所有文本以便于删除,但是只有当文本等于默认消息时才会如此?什么都找不到,任何帮助将不胜感激.

I'm making a JavaFX application and upon launch, the TextField has default text that says "Please enter this value", the user is supposed to delete said text and enter a value. However, it is quite a pain to click, drag, and highlight all the text each time. Is there a way to make it so when the user clicks on the TextField, it automatically highlights all the text for easy deletion, but ONLY when the text is equal to the default message? I researched this and couldn't find anything, any help would be much appreciated.

推荐答案

要执行您明确要求的操作,可以使用

To do what you explicitly ask, you can use

textField.focusedProperty().addListener((obs, wasFocused, isNowFocused) -> {
    if (isNowFocused && DEFAULT_TEXT.equals(textField.getText())) {
        textField.selectAll();
    }
});

但是无论如何,最好使用 promptText 来实现此功能:

But it is probably better to use promptText for this functionality anyway:

textField.setPromptText(DEFAULT_TEXT);

这篇关于单击JavaFX时突出显示TextField中的所有文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆