如何为JavaFX文本字段添加焦点的动作事件?

huangapple 未分类评论50阅读模式
标题翻译

How to add an action event to JavaFX textfield for focus?

问题

Controller:

@FXML protected void deleteInitialText(ActionEvent event){
        passwordShown.focusedProperty().addListener((ov, oldV, newV) -> {
            if(newV){
                passwordShown.setText("");
            }
        });
    }

FXML:

<TextField fx:id="passwordShown"
               text="Password"
               GridPane.columnIndex="1"
               GridPane.rowIndex="2"
               onAction="#deleteInitialText"/>

然而,这并没有产生任何结果。

英文翻译

I have a textfield for Password that has the text "Password" written in it. I want to make it so that whenever the textfield is clicked or gets focused that the text "Username" is deleted. This is the code that I have tried so far:

Controller:

@FXML protected void deleteInitialText(ActionEvent event){
        passwordShown.focusedProperty().addListener((ov, oldV, newV) -&gt; {
            if(newV){
                passwordShown.setText(&quot;&quot;);
            }
        });
    }

FXML:

&lt;TextField fx:id = &quot;passwordShown&quot;
               text = &quot;Password&quot;
               GridPane.columnIndex=&quot;1&quot;
               GridPane.rowIndex=&quot;2&quot;
               onAction=&quot;#deleteInitialText&quot;/&gt;

However, this does not produce any results.

huangapple
  • 本文由 发表于 2020年5月30日 23:43:10
  • 转载请务必保留本文链接:https://java.coder-hub.com/62104848.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定