Java 如何获取放置文档的JTextField名称?
DocumentListener是否也有类似event.getSource的功能? 我正试图改变一个JTextField的颜色,其中文本正在改变。 这是我的DocumentListener:Java 如何获取放置文档的JTextField名称?,java,swing,jtextfield,documentlistener,Java,Swing,Jtextfield,Documentlistener,DocumentListener是否也有类似event.getSource的功能? 我正试图改变一个JTextField的颜色,其中文本正在改变。 这是我的DocumentListener: DocumentListener posluchac = new DocumentListener() { public void changedUpdate(DocumentEvent e) { warn(e); } public v
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);
}
};
如果DocumentListener没有类似的
.getSource()
。如何做到这一点?你是对的,没有像其他监听器那样的getSource()
,但是你可以使用文档类的putProperty()
和getProperty()
来实现你想要的
你能行
JTextField jTextField = new JTextField("Text 1");
jTextField.getDocument().putProperty("parent", jTextField);
及
稍后在DocumentListener的事件中,您可以像这样获得父级
JTextField textField = (JTextField) e.getDocument().getProperty("parent");
其中e是
DocumentEvent
我必须使用putProperty而不是setProperty。你的回答对我帮助很大。谢谢你,好心的先生。