首页 > 解决方案 > 在 CaretListener 中获取变量(如 getActionCommand)

问题描述

当我的 JTextFiled 更新时,我想将一个变量传递给 CaretListener。

多亏了“caretUpdate”,我的听众才能完美地工作。

但是,当我使用“actionPerformed”时,我不知道如何将变量传递给我的“caretUpdate”,例如“e.getActionCommand()”,但“caretUpdate”似乎不存在,有解决方案吗?

编辑:在我看来,我有一个 textField 女巫附加到听众:

textField.setActionCommand("test");
textField.addCaretListener(this.controller);

在我的控制器中,我有监听器,我想获得值 "test" :

public void caretUpdate(CaretEvent e) {
    // here I want to get the "test" value
    String maVar = e.getActionCommand();
}

但是 CaretEvent 不存在 e.getActionCommand()。有替代方案吗?

标签: javaswinglistenerjtextfield

解决方案


推荐阅读