如何获取放置文档的JTextField名称? [英] How to get JTextField name in which is Document placed?

查看:128
本文介绍了如何获取放置文档的JTextField名称?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否还有类似于EventListener的event.getSource?
我试图改变一个JTextField的颜色,其中文本正在改变。
这是我的DocumentListener:

Is there something like event.getSource for DocumentListener too? Im trying to change color of just one JTextField in which is text changing. Here is my DocumentListener:

DocumentListener posluchac = new DocumentListener() {
        public void changedUpdate(DocumentEvent e) {
            warn(e);
        }
        public void removeUpdate(DocumentEvent e) {
            warn(e);
        }
        public void insertUpdate(DocumentEvent e) {
            warn(e);
        }
        public void warn(DocumentEvent e) {
            txtName.setBackground(Color.WHITE);
            txtSurname.setBackground(Color.WHITE);
            txtPersonalNumber.setBackground(Color.WHITE);
            txtDateOfBirth.setBackground(Color.WHITE);
        }
    };

如果没有像那样的东西.getSource()对于DocumentListener。怎么做?

If there is nothing like .getSource() for DocumentListener. How to do it?

推荐答案

你是对的,没有 getSource()和其他一些听众一样但你可以使用Document类的 putProperty() getProperty()来实现你的目标正在寻找。

You are correct, there is no getSource() like some other listeners but you can use Document class's putProperty() and getProperty() to achieve what you are looking for.

你能做什么

JTextField jTextField = new JTextField("Text 1");
jTextField.getDocument().putProperty("parent", jTextField);

稍后在DocumentListener的事件中,你可以得到这样的父母

later in DocumentListener's events, you can get the parent like this

JTextField textField = (JTextField) e.getDocument().getProperty("parent");

其中e是 DocumentEvent

这篇关于如何获取放置文档的JTextField名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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