get source of DocumentListener event

allelopath
allelopath used Ask the Experts™
on
I have a DocumentListener with 2 different JTextFields listening (as below).
How do I determine which component triggered a specific event?

DocumentListener documentListener = new DocumentListener() {
	
	public void changedUpdate(DocumentEvent documentEvent) {
		updateValue(documentEvent);
	}
	
	public void insertUpdate(DocumentEvent documentEvent) {
		updateValue(documentEvent);
	}
	
	public void removeUpdate(DocumentEvent documentEvent) {
		updateValue(documentEvent);
	}
	
	private void updateValue(DocumentEvent documentEvent) {
		
		Document source = documentEvent.getDocument();
		try {
			
		    String string = source.getText(0, source.getLength());

		} catch (BadLocationException e) {
		logger.error(e.toString());
	}

	}
};

textField1.getDocument().addDocumentListener(documentListener);
textField2.getDocument().addDocumentListener(documentListener);

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Top Expert 2016

Commented:
Try
Document source = ((JTextComponent)documentEvent.getSource()).getDocument();

Open in new window

Author

Commented:
That doesn't seem to work (doesn't compile, not sure what your intent is). I've set the names of each component, such that I am trying to do something like this:

Document source = documentEvent.getDocument();
String componentName = (((JTextComponent)source).getName());

This compiles, but gives the runtime exception:java.lang.ClassCastException: javax.swing.text.PlainDocument cannot be cast to javax.swing.text.JTextComponent
Top Expert 2016
Commented:
Sorry - that was all wrong

Use this to set a property that will allow you to distinguish between them

http://java.sun.com/javase/6/docs/api/javax/swing/text/Document.html#putProperty(java.lang.Object,%20java.lang.Object)
Build an E-Commerce Site with Angular 5

Learn how to build an E-Commerce site with Angular 5, a JavaScript framework used by developers to build web, desktop, and mobile applications.

Author

Commented:
oic. ... I'll try it...
Mick BarryJava Developer
Top Expert 2010
Commented:
us this:

if (document.getEvent()==textField1.getDocument()) {
   ...
Top Expert 2016

Commented:
>>if (document.getEvent()==textField1.getDocument()) {

Don't do that - you don't want to introduce circular dependencies - even if the reference is available
Mick BarryJava Developer
Top Expert 2010

Commented:
rotfl :) don't be ridiculous

Author

Commented:
You guys crack me up. I can't say whether the circular dependences is a problem or not, but both solutions presented are workable, and its always nice to see more the one way to do something, so I will accept both.
Top Expert 2016

Commented:
:)

>>I can't say whether the circular dependences is a problem or not

They introduce a weakness into the code. One of the reasons for using listeners is to promote loose coupling between software components. Introducing circular dependencies simply negates that loose coupling, which results in the components in question being less reusable and more difficult to maintain

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial