Javafx Textfield听众从未被调用

发布于 2025-02-05 17:57:11 字数 660 浏览 3 评论 0 原文

我有一个简单的对话框,其中一些文本字段,我想将一个简单的侦听器添加到Textfield的属性中。应该是一个不介意的,但似乎从来没有称呼它。调试表明,添加了听众(我认为),但没有任何作用。我的感觉是它收集了垃圾。问题是,为什么?

PS。添加setonKeypressed处理程序确实可以正常工作,就像上面的所有其他操作一样。

这是我的班级:

public class MyClass {
    @FXML
    private TextField emailTextField;
    @FXML
    private TextField codeTextField;
    @FXML
    private TextField nameTextField;

    private static final Logger LOG = Logger.getLogger(StageController.class.getName());

    @FXML
    private void initialize() {
        emailTextField.onKeyPressedProperty().addListener((observable, oldValue, newValue) -> {
            LOG.info("Hi!");
        });
    }

I have a simple dialog with some text fields and I want to add a simple listener to a property of the TextField. Should be a no brainer, but it's never called it seems. Debugging reveals that the listener is added (I think), but it doesn't do anything. My feeling is that it gets garbage collected. Question is, why?

ps. Adding a setOnKeyPressed handler does work fine, as do all other operations on it.

This is my class:

public class MyClass {
    @FXML
    private TextField emailTextField;
    @FXML
    private TextField codeTextField;
    @FXML
    private TextField nameTextField;

    private static final Logger LOG = Logger.getLogger(StageController.class.getName());

    @FXML
    private void initialize() {
        emailTextField.onKeyPressedProperty().addListener((observable, oldValue, newValue) -> {
            LOG.info("Hi!");
        });
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不知在何时 2025-02-12 17:57:11

您正在将 ChangElistener 添加到 onkeypressed 属性。此属性本身就是一个事件处理程序,可以响应密钥按下,而您的代码表示每当 onkeypressed 属性更改时,即任何时间 setOnKeypresse(...) > onkeypressedproperty()。设置(...)被调用,调用您的侦听器。

因此,您的侦听器永远不会被调用,因为 onkeypressedproperty()永远不会更改。

您可能不想响应 onkeypressedproperty()的更改,而是(也许)想实际响应按键按下。您可以通过设置 onkeypressed 属性,或添加另一个事件处理程序来做到这一点。

要么:

emailTextField.setOnKeyPressed(event -> LOG.info("Hi!"));

或:

emailTextField.addEventHandler(KeyEvent.KEY_PRESSED, event -> LOG.info("Hi!"));

即使这也不是您想要的。文本字段已经知道按下键时该怎么办。根据密钥,它可能会更改文本。您通常想做的是响应文本更改:

emailTextField.textProperty().addListener((obs, oldText, newText) -> LOG.info(newText));

这将响应文本更改,无论它们是否是由于钥匙按下而发生的(例如,它会响应用鼠标粘贴的用户粘贴文本),但不会响应键按不更改任何内容的按下(例如,按 shift ctrl ,或 delete 当文本字段为空时)。

You're adding a ChangeListener to the onKeyPressed property. This property is itself an event handler which responds to key presses, and your code means that whenever the onKeyPressed property changes, i.e. any time setOnKeyPressed(...) or onKeyPressedProperty().set(...) is called, your listener is invoked.

So your listener is never invoked because the onKeyPressedProperty() never changes.

You probably don't want to respond to changes in the onKeyPressedProperty(), but instead (perhaps) want to actually respond to key presses. You do this by either setting the onKeyPressed property, or adding another event handler.

Either:

emailTextField.setOnKeyPressed(event -> LOG.info("Hi!"));

or:

emailTextField.addEventHandler(KeyEvent.KEY_PRESSED, event -> LOG.info("Hi!"));

Even this is likely not what you want. The text field already knows what to do when a key is pressed; depending on the key it may change the text. What you more typically want to do is respond to the text changing:

emailTextField.textProperty().addListener((obs, oldText, newText) -> LOG.info(newText));

This will respond to text changes, whether or not they occur due to key presses (e.g. it will respond to the user pasting text with the mouse) but will not respond to key presses that don't change anything (e.g. pressing SHIFT or CTRL, or DELETE when the text field is empty).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文