How do I get a source object from documentlistener (documentevent)?

See English answers > how to find source component that generated a documentevent

public void removeUpdate( DocumentEvent arg0 ) {
   System.out.println( arg0.getDocument());
}

It will print javax swing. text. PlainDocument@49ea903c

Is there a possible way for me to get the object so I can get the value of the changed text field? At present, I only have one field, so I don't need to check, but if I use two or more fields, how do I know which jtextfield notified the listener?

Solution

I'm not sure if I can get the swing component from document But the problem is easy to solve: just add a different listener instance in each text field and store the text field in the listener itself

textField1.getDocument().addDocumentListener(new MyDocumentListener(textField1));
textField2.getDocument().addDocumentListener(new MyDocumentListener(textField2));
textField3.getDocument().addDocumentListener(new MyDocumentListener(textField3));
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>