Document Listener not working the way i would like
Hello.
I have this situation:
A Jtable and a field of JTextfields that are populated by clicking on the JTable rows or manual inserction.
All the JTextFields have a DocumentListener attached.
When i click on a row, every JTextField that is populated fires a insertUpdate and a removeUpdate event(the 1st click fires only insertUpdate).
What i want is to catch this events only when someone chances anything on a JTextField.
Ex: I click on row5, it populates the info and i change the address field, if i lose the focus or if i click on other row, i need to present a popup saying "Save or Discard changes".
Any ideas how i could implement this?
[693 byte] By [
noe.rochaa] at [2007-11-27 11:49:23]

# 1
on focus lost (FocusListener) then check the text field value against the previous value and if they are not the same...
# 2
How do i check against the old value ?
Doing that will force me to save the old values somewhere, right?
# 3
When you click on a row in the table your code should be something like:
textField.removeDocumentListener(...);
textField.setText(...);
textField.addDocumentListener(...);
Now when a DocumentEvent is generated you know it is because the user changed the text, not just because a new row was selected.
# 4
> How do i check against the old value ?
> Doing that will force me to save the old values
> somewhere, right?
Where do you keep the value that was in the table that you populate the text field with in the first place? You just pull some value out of thin air?
# 5
> When you click on a row in the table your code should
> be something like:
> textField.removeDocumentListener(...);
> textField.setText(...);
> textField.addDocumentListener(...);
> Now when a DocumentEvent is generated you know it is
> because the user changed the text, not just because a
> new row was selected.
Is there a way to get the Listener associated, i tryed
textfield.getDocument().removeDocumentListener(new MyDocumentListener())
but the component didn't lost the listener.
Message was edited by:
noe.rocha
# 6
> > How do i check against the old value ?
> > Doing that will force me to save the old values
> > somewhere, right?
>
> Where do you keep the value that was in the table
> that you populate the text field with in the first
> place? You just pull some value out of thin air?
Of course, silly me, the old values are stored on the DataModel.
Thanks.
# 7
textfield.getDocument().removeDocumentListener(new MyDocumentListener())
Is removing a new listener. You'd need to hold onto the original listener you added to remove it, if you are going to do it that way.
I still think that a focus listener might be best, as an update comes just from typing 1 character and presumably you want to popup your save message when the user is done.
# 8
Yup.
I just realized that i don't need check the value of the Textfields right way, i just need to be aware of a new row selection, tab selection (JTabbedPane), etc, one of this actions will fire a Save, Discard question (if something in the fields was changed)
Ok, just made it work using the lostFocus.
PS: But i still would like to know how to get the old listener attached to be able to remove it.
Message was edited by:
noe.rocha
# 9
Instead of calling...
tf.getDocument().addDocumentListener(new MyDocumentListener());
you do something like so...
MyDocumentListener dl = new MyDocumentListener();
tf.getDocument().addDocumentListener(dl);
...
tf.getDocument().removeDocumentListener(dl);
# 10
Just asked that because i was adding the Listener using NetBeans GUI builder.
# 11
No idea what a GUI builder does for that. Maybe it doesn't assume you want to remove a listener. Or there'd have to be some properties you can configure. Never used a GUI builder before.