让ActionListener监听JTextField中的更改而不是仅输入? [英] Let ActionListener listen for change in JTextField instead of only enter?

查看:630
本文介绍了让ActionListener监听JTextField中的更改而不是仅输入?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您可能知道,如果您有一个文本字段并且向其添加了一个ActionListener,它将只收听Enter按钮的按键。但是,我想让我的ActionListener监听文本中的更改。所以基本上我有这个:

So as you may know, if you have a text field and you add an ActionListener to it, it will only listen to the keypress of the enter button. However, I want to let my ActionListener listen to changes in text of the . So basically I've got this:

    public static JPanel mainPanel() { 
    JPanel mainp = new JPanel(); 
    JTextArea areap = new JTextArea("Some text in the textarea"); 
    JTextField fieldp = new JTextField("Edit this"); 
    areap.setEditable(false); 
    fieldp.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            // TODO Auto-generated method stub
             if(//change in textfield, for instance a letterpress or space bar)
                   { 
                        //Do this
                   } 
        }
    });
    mainp.add(areap);
    mainp.add(fieldp); 
    return mainp;
}

我可以通过任何方式收听文本中的更改(如actionPerformed事件中记录的那样) )?

Any way I can listen to changes in text (like documented in the actionPerformed event)?

推荐答案

来自 @的回答JRL

使用基础文件:

myTextField.getDocument().addDocumentListener();

这篇关于让ActionListener监听JTextField中的更改而不是仅输入?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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