如何在聚焦和聚焦时在 JavaFX TextField 上执行任务?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16549296/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How perform task on JavaFX TextField at onfocus and outfocus?
提问by java baba
I am working on JavaFX project. I need to perform some task on a JavaFX TextField
.
我正在研究 JavaFX 项目。我需要在 JavaFX 上执行一些任务TextField
。
For example on the "on focus" event for the TextField
I want to print
例如,在“TextField
我想打印”的“焦点”事件上
System.out.println("Textfield on focus");
and on the "out focus" event it should print
并且在“out focus”事件上它应该打印
System.out.println("Textfield out focus");
采纳答案by Brendan
I thought it might be helpful to see an example which specifies the ChangeListener as an anonymous inner class like scottb mentioned.
我认为看到一个将 ChangeListener 指定为匿名内部类(如提到的 scottb)的示例可能会有所帮助。
TextField yourTextField = new TextField();
yourTextField.focusedProperty().addListener(new ChangeListener<Boolean>()
{
@Override
public void changed(ObservableValue<? extends Boolean> arg0, Boolean oldPropertyValue, Boolean newPropertyValue)
{
if (newPropertyValue)
{
System.out.println("Textfield on focus");
}
else
{
System.out.println("Textfield out focus");
}
}
});
I hope this answer is helpful!
我希望这个答案有帮助!
回答by scottb
You will want to attach a ChangeListener to the FocusProperty of the TextField that you wish to monitor.
您需要将 ChangeListener 附加到要监视的 TextField 的 FocusProperty。
In JavaFX, you can attach notification events (Change or Invalidation Listeners) to any JavaFX property that an object may possess as long as the property meets the minimum definition for a JavaFX bean.
在 JavaFX 中,您可以将通知事件(Change 或 Invalidation Listeners)附加到对象可能拥有的任何 JavaFX 属性,只要该属性满足 JavaFX bean 的最低定义。
Refer to this post if your event handlers will be doing other things such as modifying Cancel or Default button settings: JavaFX 2 -- Setting the defaultButton property: mutually exclusive?
如果您的事件处理程序将执行其他操作(例如修改取消或默认按钮设置),请参阅此帖子:JavaFX 2 -- 设置 defaultButton 属性:互斥?
Here is some code to attach a Change Listener to a text box:
以下是将更改侦听器附加到文本框的一些代码:
txtDx.focusedProperty().addListener(m_txtDxListener);
The Listener object has been stored in an instance field so that it may be used with both addListener() and removeListener(). For a short lived TextField, you can specify the listener object with an anonymous inner class.
Listener 对象已存储在实例字段中,以便它可以与 addListener() 和 removeListener() 一起使用。对于短暂的 TextField,您可以使用匿名内部类指定侦听器对象。
Here is the private class that I wrote for my focus listener:
这是我为我的焦点侦听器编写的私有类:
private class FocusPropertyChangeListener implements ChangeListener<Boolean> {
FocusPropertyChangeListener() { System.out.println("New FPCL instance"); }
@Override
public void changed(ObservableValue<? extends Boolean> ov,
Boolean oldb, Boolean newb) {
System.out.println("Focus change triggered");
if (ancEncEditor.isVisible() && !ancEncEditor.isDisabled()) {
boolean b = (newb != null && newb.booleanValue() == true);
System.out.println("txtDx focus change event triggered: DxAdd = " + b);
if (b) { btnDxAdd.setDefaultButton(true); }
else { btnWindowCommit.setDefaultButton(true); }
btnWindowCommit.setCancelButton(true);
btnDxAdd.setDefaultButton(b);
}
}
}
回答by DVarga
You can use the focusedProperty
of Node
to attach a ChangeListener
.
您可以使用focusedProperty
ofNode
附加一个ChangeListener
.
Extending the answer from Brendan: from JavaFX8 (as it comes with Java 8), a lambda expression combined with a ternary operator can make it really compact:
扩展 Brendan 的答案:从 JavaFX8(Java 8 附带的)中,结合三元运算符的 lambda 表达式可以使其非常紧凑:
textField.focusedProperty().addListener((obs, oldVal, newVal) ->
System.out.println(newVal ? "Focused" : "Unfocused"));