The Artima Developer Community
Sponsored Link

Java Answers Forum
Detecting Changes in a JTextField

1 reply on 1 page. Most recent reply: Jan 8, 2004 6:43 AM by mausam

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 1 reply on 1 page
Ken Sloan

Posts: 35
Nickname: sloan
Registered: Sep, 2003

Detecting Changes in a JTextField Posted: Jan 6, 2004 2:58 PM
Reply to this message Reply
Advertisement
I need some help. I have a JTextField named 'date' and I want to kick off some code when the user changes it's value. I first tried adding an ActionListener. It compiled, but would simply not execute (which is, I believe, because the JTextField does not actually have an action associated with it like a JComboBox or JButton does...although I don't understand why the method is available, if that's the case).

Then I added a DocumentListener and tried the insertUpdate and removeUpdate methods. This really didn't work either, because they would fire as soon as a single character changed. Therefore, if the user tried to change "27-May-2004" to "27-Apr-2004", it would fire three times...as each character in the month name changed.

I finally tried a FocusListener but, again, screwy results. It would fire if the user just tabbed across the field, even when no changes were made.

All I can think of is to save the field's value when focus is gained, then compare that String to the field's value when focus is lost to see if they are different. It seems clumsy and inefficient, especially since there are many fields (not just the one date I used as an example) which need similar processing. Isn't there a clean, easy way to detect when the full character string in a JTextField has changed?

Thanks in advance for any help anyone can provide. This has been a frustrating few days messing with this.


mausam

Posts: 243
Nickname: mausam
Registered: Sep, 2003

Re: Detecting Changes in a JTextField Posted: Jan 8, 2004 6:43 AM
Reply to this message Reply
I think that should be the way you are doing.

What you exactly want is a mixture of documentlistner and focus listener.

And you are doing that exactly on focus lost.

You can also do it in other way.

But that may not be better than the way u are doing
Take a boolean dataChanged as false;

and in document listener make this boolean as true once documentis changed.

Then at focus changed you will be knowing that document was changed and can process with ur data.

Flat View: This topic has 1 reply on 1 page
Topic: join()  ??? Previous Topic   Next Topic Topic: string manipulations

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use